将照片上传到flickr的签名无效

时间:2011-01-10 17:12:13

标签: php flickr

我使用以下代码:

<?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;
?>

为flickr上传后调用创建我的签名。但是,它一直告诉我它无效......我做错了什么?

也许你可以快速查看我的代码?

3 个答案:

答案 0 :(得分:1)

您需要在字符串中按字母顺序包含除photo参数之外的所有帖子参数。

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

答案 1 :(得分:0)

我遇到了同样的问题,尝试了所有订单和参数的组合,但最后我发现我在html enctype="multipart/form-data"标签中缺少FORM,这需要像这样:

<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>

答案 2 :(得分:0)

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];
            }
        }

    }

希望得到这个帮助。