如何使用Flickr API上传照片? [英] How to upload a photo using the Flickr API?

查看:177
本文介绍了如何使用Flickr API上传照片?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm unable to upload an image to Flickr using the Flickr API. I'm trying to do this in a web-based application running PHP.

我尝试了 DPZFlickr ,在尝试上传时会返回以下内容:

I've tried DPZFlickr, which returns this, when trying to upload:

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

检查Flickr API的响应,与尝试 Dantsu版本的phpFlickr时返回的内容相同,它返回以下内容:

Inspecting the Flickr API's response, it's the same as what is returned when trying Dantsu's version of phpFlickr, which returns this:

oauth_problem=signature_invalid&debug_sbs=...

我已经滚动了自己的CURL,它将返回以下内容:

I've rolled my own CURL, which returns this:

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

Here's the full code I use for creating my CURL request (edited to use 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;

我很茫然.如何进行这项工作?

I'm at a loss. How to make this work?

推荐答案

此问答让我朝正确的方向前进.

This question and answer got me going in the right direction.

我仍然无法启动和运行自己的CURL,但是我设法破解了 DPZFlickr的库即可正常运行.

I still was unable to get my own CURL up and running, but I managed to hack a few lines of code in DPZFlickr's library to get that working.

我将httprequest函数更新为:

I updated the httprequest function to this:

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.

Changing how the photo is included. Now through CurlFile.

我正在PHP 7.2上执行此操作.显然,CURLOPT_SAFE_UPLOAD在PHP 5.x中进行了一些更改,而CurlFile在PHP 7.1中成为必需.

I'm doing this on PHP 7.2. Apparently, the CURLOPT_SAFE_UPLOAD underwent some changes in PHP 5.x, while CurlFile became a requirement in PHP 7.1.

这篇关于如何使用Flickr API上传照片?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆