Search code examples
phpapiimgur

PHP upload to a imgur Album does not work?


im trying to upload to my imgur Account's album, however, the upload works, but it looks like its gonna uploaded as anonym/public?

here's my code:

<?php

$client_id = "465xxx8c44294";
$image = file_get_contents("../images/d4487317c3xxx93210b293c2e.jpg");

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://api.imgur.com/3/image.json');
curl_setopt($ch, CURLOPT_POST, TRUE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Authorization: Client-ID ' . $client_id));
//curl_setopt($ch, CURLOPT_POSTFIELDS, array('image' => base64_encode($image)));
curl_setopt($ch, CURLOPT_POSTFIELDS, array('image' => base64_encode($image), 'album' => 'nqxxxGE'));

$reply = curl_exec($ch);
curl_close($ch);

$reply = json_decode($reply);
printf('<img height="180" src="%s" >', $reply->data->link);

any advice?


Solution

  • If you want to add image to album, according doc, you need to pass album id. Make sure you've generated token that has access to secret albums. Here you can find some tips about tokens.

    curl_setopt($ch, CURLOPT_POSTFIELDS, 
        array(
            'image' => base64_encode($image), 
            'album' => '5' // 5 - your album id
        ) 
    ); 
    

    You can check you albums id using this api.

    To refresh token:

    If a user has authorized their account but you no longer have a valid access_token for them, then a new one can be generated by using the refresh_token.

    To obtain a new access token, your application performs a POST to https://api.imgur.com/oauth2/token. The request must include the following parameters to use a refresh token:

    refresh_token: The refresh token returned from the authorization code exchange

    client_id: The client_id obtained during application registration

    client_secret: The client secret obtained during application registration.

    grant_type: As defined in the OAuth2 specification, this field must contain a value of: refresh_token.

    As long as the user has not revoked the access granted to your application, the response includes a new access token. A response from such a request is shown below:

    {
        "access_token":"5c3118ebb73fbb275945ab340be60b610a3216d6",
        "refresh_token":"d36b474c95bb9ee54b992c7c34fffc2cc343d0a7",
        "expires_in":3600,
        "token_type":"Bearer",
        "account_username":"saponifi3d"
    }
    

    Add the refresh part at the start of your script. Something like:

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, 'https://api.imgur.com/oauth2/token');
    curl_setopt($ch, CURLOPT_POST, TRUE);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
    
    curl_setopt($ch, CURLOPT_POSTFIELDS, [
        'refresh_token' => $refreshToken, // Your refresh_token
        'client_id' => $client_id,
        'client_secret' => $clientSecret, //Your client_secret
        'grant_type' =>  'refresh_token'
    ]);
    
    //Keep in mind that refreshToken and clientSecret are obtained during registration.
    
    $reply = curl_exec($ch);
    curl_close($ch);
    
    $reply = json_decode($reply);
    $accessToken = $reply->access_token;