Skip to content

Instantly share code, notes, and snippets.

@mgng
Created September 12, 2014 08:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mgng/54288fc4643f365c6acf to your computer and use it in GitHub Desktop.
Save mgng/54288fc4643f365c6acf to your computer and use it in GitHub Desktop.
フォト蔵 API 使って画像をアップロードするやつ
<?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