AFS AFS - 9 months ago 212
PHP Question

Request instagram api acces token with cURL

I'm trying to get the acces token from instagram api this is the sample request of the documentation

curl -F 'client_id=CLIENT_ID' \
-F 'client_secret=CLIENT_SECRET' \
-F 'grant_type=authorization_code' \
-F 'code=CODE' \

and this is my code

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "");
curl_setopt($curl,CURLOPT_POST, true);
curl_setopt($curl,CURLOPT_POSTFIELDS, "client_id=MYID&
curl_setopt($curl, CURLOPT_RETURNTRANSFER,true);
curl_setopt($curl, CURLOPT_TIMEOUT, 10);
$output = curl_exec($curl);

echo ($output);

With this code I don't get anything from curl request


Using this code will give you the Access Token in the redirecting URL.

$client_id = 'YOUR CLIENT ID';
    $client_secret ='YOUR CLIENT SECRET';
    $redirect_uri = 'YOUR REDIRECT URI';

     $auth_request_url = ''.$client_id.'&redirect_uri='.$redirect_uri .'&response_type=token';
/* Send user to authorisation */
header("Location: ".$auth_request_url);

Also if you want the Access Token programmatically use this one

$client_id = 'YOUR CLIENT ID';
    $client_secret ='YOUR CLIENT SECRET';
        $redirect_uri = 'YOUR REDIRECT URI';
    $code ='Enter your code manually';

    $url = "";
    $access_token_parameters = array(
        'client_id'                =>     $client_id,
        'client_secret'            =>     $client_secret,
        'grant_type'               =>     'authorization_code',
        'redirect_uri'             =>     $redirect_uri,
        'code'                     =>     $code

$curl = curl_init($url);    // we init curl by passing the url
    curl_setopt($curl,CURLOPT_POST,true);   // to send a POST request
    curl_setopt($curl,CURLOPT_POSTFIELDS,$access_token_parameters);   // indicate the data to send
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);   // to return the transfer as a string of the return value of curl_exec() instead of outputting it out directly.
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);   // to stop cURL from verifying the peer's certificate.
    $result = curl_exec($curl);   // to perform the curl session
    curl_close($curl);   // to close the curl session


NOTE: $url and $redirect_uri are not same