Smugmug使用PHP& amp;上传图像API v2卷曲

时间:2015-07-07 14:22:34

标签: php curl upload

我尝试使用Upload API v2将图像上传到Smugmug。 我已经尝试了很长一段时间,但它不会工作。 这是我到目前为止所提出的。

当我运行脚本时,响应如下:

    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();

    $path = 'image.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);


    $header[] = 'Authorization: OAuth realm="http://upload.smugmug.com/",
                 oauth_consumer_key='.$parameters['oauth_consumer_key'].',
                 oauth_token='.$parameters['oauth_token'].',
                 oauth_signature_method='.$parameters['oauth_signature_method'].',
                 oauth_signature='.$parameters['oauth_signature'].',
                 oauth_timestamp='.time().',
                 oauth_nonce='.md5(time() . mt_rand()).',
                 oauth_version=1.0';

    $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: image.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);

但该文件尚未上传.. 你看到代码有什么问题吗?

<div id="container" class="container j-container"><div class="row"><?php echo $column_left; ?> <?php echo $column_right; ?>
<?php if ($column_left && $column_right) { ?>
<?php $class = 'col-sm-6'; ?>
<?php } elseif ($column_left || $column_right) { ?>
<?php $class = 'col-sm-9'; ?>
<?php } else { ?>
<?php $class = 'col-sm-12'; ?>
<?php } ?>
<div id="content" class="<?php echo $class; ?>"><?php echo $content_top; ?><?php echo $content_bottom; ?></div>
</div>

1 个答案:

答案 0 :(得分:-1)

使用OAuth可能很棘手,SmugMug的上传API有点独特。使用其中一个PHP包装器而不是编写自己的代码可能会有所帮助。以下是两个,其中包含如何上传图像的示例:

1。)phpSmug

2。)DFM Smug Wrapper

例如使用DFM Smug Wrapper,您可以执行以下操作(从包装器附带的README文件中复制):

<?php
require_once("dfm-smug-wrapper/dfm-smug-wrapper.php");
$f = new DFM_Smug(
       "oauth_consumer_key=12345678",
       "oauth_secret=some_secret",
       "token_id=12345",
       "token_secret=some_other_secret",
       "app_name=My Cool App/1.0 (http://app.com)",
       "api_ver=2.0"
  );
$f->images_upload("AlbumID=123456", "File=/path/to/image.jpg");
?>