简体   繁体   中英

Getting Access token from Instagram for API on PHP

I have got the code using url: https://api.instagram.com/oauth/authorize/?client_id=CLIENT-ID&redirect_uri=REDIRECT-URI&response_type=code

After that on Instagram Documentation this code is given:

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

How to use it in PHP?

    $fields = array(
           'client_id'     => 'YOUR-CLIENT-ID',
           'client_secret' => 'YOUR-CLIENT-SECRET',
           'grant_type'    => 'authorization_code',
           'redirect_uri'  => 'YOUR-REDIRECT-URI',
           'code'          => 'YOUR-CODE'
    );
    $url = 'https://api.instagram.com/oauth/access_token';
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_TIMEOUT, 20);
    curl_setopt($ch,CURLOPT_POST,true); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $fields);
    $result = curl_exec($ch);
    curl_close($ch); 
    $result = json_decode($result);
    return $result->access_token; //your token

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