简体   繁体   English

无效的签名上传照片到flickr

[英]Invalid signature uploading photo to flickr

I am using the following code: 我正在使用以下代码:

<?php
 $token = $_REQUEST['token'];
 $file = $_REQUEST['file'];
 $sig = "xxxxxxxxxxxxxxxxxapi_keyxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxauth_token" . $token;
 $sign = md5($sig);
 $url = "http://api.flickr.com/services/upload/";
 $ch = curl_init();
 /**
 * Set the URL of the page or file to download.
 */

 $body = "api_key=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx&api_sig=" . $sign .         "&auth_token=" . $token . "&photo=" . $file;

 curl_setopt($ch, CURLOPT_URL, $url);

 curl_setopt($ch, CURLOPT_POST, 1);
 curl_setopt($ch, CURLOPT_POSTFIELDS, $body);

 /**
 * Ask cURL to return the contents in a variable
 * instead of simply echoing them to the browser.
 */
 curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
 /**
 * Execute the cURL session
 */
 $contents = curl_exec ($ch);
 /**
 * Close cURL session
 */
 curl_close ($ch);

 echo $contents;
?>

to create my signature for the flickr upload post call. 为flickr上传帖子通话创建我的签名。 However, it keeps telling me its invalid....what am I doing wrong? 但是,它不断告诉我它无效。...我在做什么错?

maybe you could have a quick look at my code? 也许您可以快速浏览一下我的代码?

You need to include all of the post parameters, except the photo parameter, in the string (in alphabetical order) that you take the hash of. 您需要在获取哈希值的字符串(按字母顺序)中包括除photo参数以外的所有post参数。

See http://www.flickr.com/services/api/auth.spec.html#signing 请参阅http://www.flickr.com/services/api/auth.spec.html#signing

I just had the same problem, tried all combinations of orders and parameters but in the end it turned out that I was missing enctype="multipart/form-data" inside html FORM tag which needs to be like this: 我只是遇到了同样的问题,尝试了命令和参数的所有组合,但最终结果证明我在html FORM标签中缺少enctype="multipart/form-data" ,它需要像这样:

<FORM action="http://api.flickr.com/services/upload/" enctype="multipart/form-data" method="post">
        <P>     
        <LABEL for="api_key">api_key: </LABEL>
                  <INPUT type="text" id="Text1" name="api_key"><BR>
        <LABEL for="auth_token">auth_token: </LABEL>
                  <INPUT type="text" id="Text2" name="auth_token"><BR>
        <LABEL for="api_sig">api_sig: </LABEL>
                  <INPUT type="text" id="Text3" name="api_sig"><BR>
        <LABEL for="photo">photo: </LABEL>
                  <INPUT type="file" id="Password1" name="photo"><BR>
        <INPUT type="submit" value="Send"> <INPUT type="reset">
        </P>
     </FORM>
public function sync_upload($photo, $title = null, $description = null, $tags = null, $is_public = null, $is_friend = null, $is_family = null)
    {
        $args = array("api_key" => $this->api_key, "title" => $title, "description" => $description, "tags" => $tags, "is_public" => $is_public, "is_friend" => $is_friend, "is_family" => $is_family);

        if (!empty($this->token)) {
            $args = array_merge($args, array("auth_token" => $this->token));
        }
        ksort($args);
        $auth_sig = "";
        foreach ($args as $key => $data) {
            if ( is_null($data) ) {
                unset($args[$key]);
            } else {
                $auth_sig .= $key . $data;
            }
        }
        if (!empty($this->secret)) {
            $api_sig = md5($this->secret . $auth_sig);
            $args["api_sig"] = $api_sig;
        }
        $photo = realpath($photo);
        $args['photo'] = new CurlFile($photo, 'image/png');
        $curl = curl_init($this->upload_endpoint);
        curl_setopt($curl, CURLOPT_POST, true);
        curl_setopt($curl,CURLOPT_SSL_VERIFYPEER,false);
        curl_setopt($curl,CURLOPT_SSLVERSION,4);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $args);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        $response = curl_exec($curl);
        curl_close($curl);

        $rsp = explode("\n", $response);
        foreach ($rsp as $line) {
            if (preg_match('|<err code="([0-9]+)" msg="(.*)"|', $line, $match)) {
               return false;
            } elseif (preg_match("|<photoid>(.*)</photoid>|", $line, $match)) {
                return $match[1];
            }
        }

    }

Hope this help. 希望对您有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM