如何使用 Flickr API 上传照片?

How to upload a photo using the Flickr API?

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

我已经尝试 DPZFlickr,其中 returns 这个,在尝试上传时:

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

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

oauth_problem=signature_invalid&debug_sbs=...

我推出了自己的 CURL,returns 这个:

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;

我很茫然。如何实现?

让我朝着正确的方向前进。

我仍然无法启动自己的 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 中成为一项要求。