0

I have this sample code which is required for the Instagram authentication. i don't know how to do this curl in php. The step below is what i need to achieve.

Step Three: Request the access_token

Now you need to exchange the code you have received in the previous step for an access token. In order to make this exchange, you simply have to POST this code, along with some app identification parameters, to our access_token endpoint. These are the required parameters:

client_id: your client id client_secret: your client secret grant_type: authorization_code is currently the only supported value redirect_uri: the redirect_uri you used in the authorization request. Note: this has to be the same value as in the authorization request. code: the exact code you received during the authorization step. This is a sample request:

curl -F 'client_id=CLIENT_ID' \
-F 'client_secret=CLIENT_SECRET' \
-F 'grant_type=authorization_code' \
-F 'redirect_uri=AUTHORIZATION_REDIRECT_URI' \
-F 'code=CODE' \
https://api.instagram.com/oauth/access_token

If successful, this call will return a neatly packaged OAuth Token that you can use to make authenticated calls to the API. We also include the user who just authenticated for your convenience:

{ "access_token": "fb2e77d.47a0479900504cb3ab4a1f626d174d2d", "user": { "id": "1574083", "username": "snoopdogg", "full_name": "Snoop Dogg", "profile_picture": "..." } }

I am new to this. so i need help

sahad kkt
  • 3
  • 1
  • 4

2 Answers2

1

Try this code

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

    $url = "https://api.instagram.com/oauth/access_token";
    $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

     var_dump($result);
Rahul
  • 2,374
  • 2
  • 9
  • 17
0
$url = 'Enter the user here';
        $myvars = 'secret=' . $secretKey. '&remoteip=' . $ip;//These are parameters

        $ch = curl_init($url);//initialise
        curl_setopt($ch, CURLOPT_POST, 1);//post request
        curl_setopt($ch, CURLOPT_POSTFIELDS, $myvars);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

        $response = curl_exec($ch);//Execute curl request
        $result = json_decode($response);//decode the json response

I hope this may help

User123123
  • 19
  • 1
  • 7