Skip to content

Instantly share code, notes, and snippets.

@mgng
Last active Dec 12, 2015
Embed
What would you like to do?
'import_http_files' => array(
'robot' => '/http/import',
'url' => array(
'http://cdn-ak.f.st-hatena.com/images/fotolife/m/mgng/20130130/20130130125239.png',
'http://cdn-ak.f.st-hatena.com/images/fotolife/m/mgng/20130130/20130130105748.png',
),
),
'import_http_files' => array(
'robot' => '/html/convert',
'url' => 'http://google.com',
),
<?php
/**
* transloadit API テスト
*/
// via: https://transloadit.com/accounts/credentials
$auth_key = 'メモったAuth Key';
$auth_sec = 'メモったAuth Secret';;
// params と signature を作成
$params = json_encode( array(
// 認証用
'auth' => array(
'expires' => gmdate( 'Y/m/d H:i:s+00:00', strtotime( '+1 hour' ) ),
'key' => $auth_key,
),
// 処理内容
'steps' => array(
// web上のファイル使いますよ宣言
'import_http_files' => array(
'robot' => '/http/import',
'url' => array(
'http://cdn-ak.f.st-hatena.com/images/fotolife/m/mgng/20130130/20130130125239.png',
'http://cdn-ak.f.st-hatena.com/images/fotolife/m/mgng/20130130/20130130105748.png',
),
),
// import_http_files に指定した画像を 100*100 にリサイズしますよ宣言
'resize_images' => array(
'use' => 'import_http_files',
'robot' => '/image/resize',
'format' => 'jpg',
'width' => 100,
'height' => 100,
),
)
));
$signature = hash_hmac( 'sha1', $params, $auth_sec );
// API に params と signature を POST して認証、assembly_url を取得
$auth = json_decode( post( 'http://api2.transloadit.com/assemblies', array(
'params' => $params,
'signature' => $signature,
) ) );
$assembly_url = $auth->assembly_url;
// sleep(1) しつつ assembly_url にリクエスト投げて処理状況確認
// ASSEMBLY_COMPLETED になってたら URL 表示
for( $i=0; $i<5; $i++ ) {
sleep(1);
$res = json_decode( file_get_contents( $assembly_url ) );
if ( $res->ok === 'ASSEMBLY_COMPLETED' ) {
foreach( $res->results->resize_images as $image ) {
echo $image->url, "\n";
}
break;
}
}
/**
* 簡易POST関数
* @param string $url
* @param array $data OPTIONAL
* @return string
*/
function post ( $url, $data = array() ) {
return file_get_contents( $url, false, stream_context_create(
array(
'http'=>array(
'method' => 'POST',
'header' => "Content-type: application/x-www-form-urlencoded\r\n",
'content'=> http_build_query( $data ),
)
)
));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment