【问题标题】:How to upload a photo using the Flickr API?如何使用 Flickr API 上传照片?
【发布时间】:2019-01-23 16:21:10
【问题描述】:

我无法使用 Flickr API 将图像上传到 Flickr。我正在尝试在运行 PHP 的基于 Web 的应用程序中执行此操作。

我尝试过DPZFlickr,它在尝试上传时返回:

Array ( [stat] => fail [err] => Array ( ) )

检查 Flickr API 的响应,它与尝试 Dantsu's version of phpFlickr 时返回的相同,返回如下:

oauth_problem=signature_invalid&debug_sbs=...

我滚动了自己的 CURL,它返回:

Upload result: HTTP/1.1 200 OK Date: Wed, 23 Jan 2019 16:09:39 GMT Content-Type: text/xml; charset=utf-8 Content-Length: 109 P3P: policyref="https://policies.yahoo.com/w3c/p3p.xml", CP="CAO...

这是我用于创建 CURL 请求的完整代码(编辑为使用 CurlFile):

$consumerSecret = $flickrApiSecret;
$ap_url = "https://up.flickr.com/services/upload/";
$apiKey = $flickrApiKey;
$oauth_nonce = time();
$oauthToken = $_SESSION["FlickrSessionOauthData"]["oauth_request_token"];
$oauthTokenSecret = $_SESSION["FlickrSessionOauthData"]["oauth_request_token_secret"];
$text = "Test";
$signatureMethod = "HMAC-SHA1";
$oauthVersion = "1.0";
$timestamp = time();

$parms  = "description=".$text."&format=json&oauth_consumer_key=".$apiKey."&oauth_nonce=".$oauth_nonce;
$parms .= "&oauth_signature_method=".$signatureMethod."&oauth_timestamp=".$timestamp."&oauth_token=".$oauthToken;
$parms .= "&oauth_version=".$oauthVersion."&title=".$text;

$baseString  = "";
$baseString .= "POST&".urlencode($ap_url)."&".urlencode($parms);

$hashkey = $consumerSecret."&".$oauthTokenSecret;
$apiSignature = base64_encode(hash_hmac('sha1', $baseString, $hashkey, true));

$filePath = "/path_to_file/0.jpg";  
$postFields["description"] = $text;
$postFields["format"] = "json";
$postFields["photo"] = new \CurlFile($filePath, mime_content_type ( $filePath ), 'photo');
$postFields["title"] = $text;

print_r ($postFields);
print "<br />";

$url = "https://up.flickr.com/services/upload/";

$oauth_header = "oauth_consumer_key=".$apiKey.",oauth_nonce=".$oauth_nonce.",oauth_signature_method=".$signatureMethod.",oauth_timestamp=".$timestamp.",oauth_token=".$oauthToken.",oauth_version=".$oauthVersion.",oauth_signature=".$apiSignature;

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array("Authorization: OAuth ".$oauth_header));
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_VERBOSE, 0);
curl_setopt($curl, CURLOPT_HEADER, 1);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl, CURLOPT_POST, TRUE);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postFields);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SAFE_UPLOAD, true);
$response = curl_exec ($curl);
curl_close ($curl); 
echo $response;

我很茫然。如何使这项工作?

【问题讨论】:

  • 我猜你使用的是 php >= 7.1,不幸的是 curl 已经弃用了旧的发送文件的方式,所以你需要使用。更多here
  • 谢谢@Viney。我使用 CurlFile 更新了代码(并将 CURLOPT_SAFE_UPLOAD 设置为良好的衡量标准),但我从 Flickr 得到的响应完全相同。

标签: php upload flickr


【解决方案1】:

This question and answer 让我朝着正确的方向前进。

我仍然无法启动并运行我自己的 CURL,但我设法破解了 DPZFlickr's library 中的几行代码以使其正常运行。

我将 httprequest 函数更新为:

private function httpRequest($url, $parameters)
{
    if (isset($parameters["photo"])) {
        $p = $parameters["photo"];
        $pf = substr($p, 1);

        $parameters["photo"] = new \CurlFile($pf, mime_content_type ( $pf ), 'photo');
    }

    $curl = curl_init();

    curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
    curl_setopt($curl, CURLOPT_TIMEOUT, $this->httpTimeout);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($curl, CURLOPT_SSLVERSION, CURL_SSLVERSION_TLSv1);

    if ($this->method == 'POST')
    {
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_POST, TRUE);
        curl_setopt($curl, CURLOPT_POSTFIELDS, $parameters);
    }
    else
    {
        // Assume GET
        curl_setopt($curl, CURLOPT_URL, "$url?" . $this->joinParameters($parameters));
    }

    $response = curl_exec($curl);
    $headers = curl_getinfo($curl);

    curl_close($curl);

    $this->lastHttpResponseCode = $headers['http_code'];

    return $response;
}

这包括一项更改:

更改包含照片的方式。现在通过 CurlFile。

我在 PHP 7.2 上执行此操作。显然,CURLOPT_SAFE_UPLOAD 在 PHP 5.x 中发生了一些变化,而 CurlFile 在 PHP 7.1 中成为了一项要求。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-07-05
    • 1970-01-01
    • 2011-12-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多