【发布时间】: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 得到的响应完全相同。