简体   繁体   中英

How to upload a photo using the Flickr API?

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.

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

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

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

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

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.

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.

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

This includes one change:

Changing how the photo is included. Now through CurlFile.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM