Created
September 12, 2014 08:10
-
-
Save mgng/54288fc4643f365c6acf to your computer and use it in GitHub Desktop.
フォト蔵 API 使って画像をアップロードするやつ
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
フォト蔵 API 使って画像をアップロードするやつ | |
Usage: | |
$userid = "フォト蔵に登録したメールアドレス"; | |
$password = "パスワード"; | |
$Photozou = new \Photozou( $userid, $password ); | |
// パラメータ詳細は http://photozou.jp/basic/api_method_photo_add を参照 | |
$params = array( | |
"photo" => "/path/to/test.jpg", | |
"album_id" => "アップロード先のアルバムID", | |
"photo_title" => "test.jpg アップロードテスト", | |
"date_type" => "date", | |
"year" => 2014, | |
"month" => 9, | |
"day" => 12, | |
); | |
$result = $Photozou->photoAdd( $params ); | |
print_r( json_decode( $result ) ); | |
Todo: | |
画像形式チェックとかファイル名チェックとか | |
HTTPレスポンスヘッダ確認とか | |
*/ | |
class Photozou { | |
const API_REST_URL = "https://api.photozou.jp/rest/"; | |
const CRLF = "\r\n"; | |
protected $_userid = null; | |
protected $_password = null; | |
public function __construct( $userid, $password ) { | |
$this->_userid = $userid; | |
$this->_password = $password; | |
return true; | |
} | |
public function photoAdd( $params ) { | |
$boundary = md5( microtime( true ) ); | |
$data = array(); | |
foreach( $params as $key => $value ) { | |
$data[] = "--{$boundary}"; | |
if ( $key !== "photo" ) { | |
$data[] = "Content-Disposition: form-data; name=\"{$key}\"" . self::CRLF; | |
$data[] = $value; | |
} else { | |
$fileinfo = getimagesize( $value ); | |
$filename = basename( $value ); | |
$data[] = "Content-Disposition: form-data; name=\"{$key}\"; filename=\"{$filename}\""; | |
$data[] = $fileinfo["mime"] . self::CRLF; | |
$data[] = file_get_contents( $value ); | |
} | |
} | |
$data[] = "--{$boundary}--"; | |
return file_get_contents( self::API_REST_URL . "photo_add.json", false, stream_context_create( array( | |
"http" => array( | |
"method" => "POST", | |
"content" => implode( self::CRLF, $data ), | |
"header" => implode( self::CRLF, array( | |
"Authorization: Basic " . base64_encode( "{$this->_userid}:{$this->_password}" ), | |
"Content-Type: multipart/form-data; boundary={$boundary}", | |
)), | |
), | |
))); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment