Upload API v2 PHP CURL
smugpho
Registered Users Posts: 1 Beginner grinner
Hello,
I'm trying to upload images using the v2 API but i can't seem to get it to work.
Can anyone see whats going wrong?
[PHP]
include_once("oauth/OAuth.php");
include_once("constants.php");
$access_token = '....';
$access_token_secret = '.....';
$consumer = new OAuthConsumer(API_KEY, API_KEY_SECRET);
$signature_method = new OAuthSignatureMethod_PLAINTEXT;
$token = new OAuthToken($access_token, $access_token_secret);
$req_token = OAuthRequest::from_consumer_and_token($consumer, $token, "POST", 'http://upload.smugmug.com');
$req_token->sign_request($signature_method, $consumer, $token);
$parameters = $req_token->get_parameters();
$header[] = 'Authorization: OAuth realm="http://upload.smugmug.com/",
oauth_consumer_key='.$parameters.',
oauth_token='.$parameters.',
oauth_signature_method='.$parameters.',
oauth_signature='.$parameters.',
oauth_timestamp='.time().',
oauth_nonce='.md5(time() . mt_rand()).',
oauth_version=1.0';
$path = 'barcode.png';
$type = pathinfo($path, PATHINFO_EXTENSION);
$data = file_get_contents($path);
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_file($finfo, $path);
finfo_close($finfo);
$body[] = 'Accept: application/json';
$body[] = 'X-Smug-Version: v2';
$body[] = 'X-Smug-ResponseType: JSON';
$body[] = 'X-Smug-AlbumUri: /api/v2/album/******';
$body[] = 'X-Smug-Filename: iamge.png';
$body[] = 'Content-MD5: '.$base64.'';
$body[] = 'Content-Length: '.filesize($path).'';
$body[] = 'Content-Type: '.$mime.'';
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_URL, 'http://upload.smugmug.com/');
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_error ($ch);
$result = curl_exec($ch);
print_r($result);
[/PHP]
I'm trying to upload images using the v2 API but i can't seem to get it to work.
Can anyone see whats going wrong?
[PHP]
include_once("oauth/OAuth.php");
include_once("constants.php");
$access_token = '....';
$access_token_secret = '.....';
$consumer = new OAuthConsumer(API_KEY, API_KEY_SECRET);
$signature_method = new OAuthSignatureMethod_PLAINTEXT;
$token = new OAuthToken($access_token, $access_token_secret);
$req_token = OAuthRequest::from_consumer_and_token($consumer, $token, "POST", 'http://upload.smugmug.com');
$req_token->sign_request($signature_method, $consumer, $token);
$parameters = $req_token->get_parameters();
$header[] = 'Authorization: OAuth realm="http://upload.smugmug.com/",
oauth_consumer_key='.$parameters.',
oauth_token='.$parameters.',
oauth_signature_method='.$parameters.',
oauth_signature='.$parameters.',
oauth_timestamp='.time().',
oauth_nonce='.md5(time() . mt_rand()).',
oauth_version=1.0';
$path = 'barcode.png';
$type = pathinfo($path, PATHINFO_EXTENSION);
$data = file_get_contents($path);
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
$finfo = finfo_open(FILEINFO_MIME_TYPE);
$mime = finfo_file($finfo, $path);
finfo_close($finfo);
$body[] = 'Accept: application/json';
$body[] = 'X-Smug-Version: v2';
$body[] = 'X-Smug-ResponseType: JSON';
$body[] = 'X-Smug-AlbumUri: /api/v2/album/******';
$body[] = 'X-Smug-Filename: iamge.png';
$body[] = 'Content-MD5: '.$base64.'';
$body[] = 'Content-Length: '.filesize($path).'';
$body[] = 'Content-Type: '.$mime.'';
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_URL, 'http://upload.smugmug.com/');
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_error ($ch);
$result = curl_exec($ch);
print_r($result);
[/PHP]
0
Comments
Sorcerer and API Guy at SmugMug
Since that header is optional, I would suggest that you first try removing it.
Sorcerer and API Guy at SmugMug