0

I am trying to the Instagram PHP API to get friends photos. I registered as a sandbox user as well as a dummy user with few random images posted.Then, I invited the dummy user to grant the app in sandbox to connect his account.

To get access token I send a request to get code and then get access token with scope:

https://api.instagram.com/oauth/access_token?client_id=CLIENT_ID&client_secret=SECRET_STRING&grant_type=authorization_code&redirect_uri=http://localhost/&code=CODE&scope=basic+public_content+comments+likes`

PHP code:

$url = 'https://api.instagram.com/oauth/access_token';
    $payload = [
            $GLOBALS['KEY_CID'] => $GLOBALS['VAL_CID'],
            $GLOBALS['KEY_CECRET'] => $GLOBALS['VAL_CECRET'],
            'grant_type' => 'authorization_code',
            'redirect_uri' => 'http://localhost/',
            'code' => $param_code,
            'scope' => 'basic+public_content+comments+likes'];

$ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url); // url
        curl_setopt($ch, CURLOPT_POST, true); // POST
        curl_setopt($ch, CURLOPT_POSTFIELDS, $payload); // POST DATA
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // RETURN RESULT true
        curl_setopt($ch, CURLOPT_HEADER, 0); // RETURN HEADER false
        curl_setopt($ch, CURLOPT_NOBODY, 0); // NO RETURN BODY false / we need the body to return
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); // VERIFY SSL HOST false
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // VERIFY SSL PEER false
$response = json_decode(curl_exec($ch), true);
curl_close($ch);

After I got access token successfully.

I try to get images from a sandbox user which is a dummy account I created for testing:

https://api.instagram.com/v1/users/fane.leee/media/recent?access_token=ACCESS_TOKEN

PHP code for retrieving image:

$url = 'https://api.instagram.com/v1/users/fane.leee/media/recent?access_token='.$ACCESS_TOKEN;
    $ch = curl_init(); // initializing
    curl_setopt( $ch, CURLOPT_URL, $url ); // API URL to connect
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1 ); // return the result, do not print
    curl_setopt( $ch, CURLOPT_TIMEOUT, 30 );
    $response = json_decode(curl_exec($ch), true); // connect and get json data
    curl_close($ch);

The problem
The $response of the get image request is NULL without error message. Anyone got any ideas about the problem?

Thanks.


Reference

  1. Instagram development doc authentication and authorization.

  2. Instagram development doc about user endpoints

  3. Another StackOverflow post about Instagram web API


Development environment: Ubuntu 16.04
Apache2
PHP 7.0.8
MySQL 14.14


Edit 1: Add the PHP code calling cUrl functions.
Edit 2: If I replace the user id 'fane.leee' by using 'self', I am able to retrieve the images I posted. I also followed the user 'fane.leee'. Any else I should do to retrieve the friends' media?

Community
  • 1
  • 1
r0n9
  • 2,505
  • 1
  • 29
  • 43
  • Post some code please. – Sub 6 Resources Nov 17 '16 at 02:22
  • Thanks for your comments, code added :) – r0n9 Nov 17 '16 at 02:30
  • 1
    Have you added any sandbox users? – Vinay Nov 17 '16 at 02:45
  • Yes, as I described in the first paragraph, I created a dummy account, post few images and add the dummy account as a sandbox user. As the result, the dummy use's user id is listed in the `sandbox users` list. So there are two users in the `sandbox users` list under the admin name, one is the `admin` the other is the dummy user who is accepted the invitation. Cheers :) – r0n9 Nov 17 '16 at 03:28
  • 1
    From the documentation you posted, it looks like: `$GLOBALS['KEY_CID'] => $GLOBALS['VAL_CID'], $GLOBALS['KEY_CECRET'] => $GLOBALS['VAL_CECRET'],` should be: ` 'client_id' => $GLOBALS['VAL_CID'], 'client_secret' => $GLOBALS['VAL_CECRET'], ` – fredrover Nov 18 '16 at 02:12
  • I defined some global variables somewhere else. (also realize those are kind of pointless later) The value of those variable are 'client_id' and 'client_secret'. I was copy and paste the code and forgot to replace variables by their values. Since I could use the token to get media from the user 'self'. I thought the token query should be good. Thanks – r0n9 Nov 18 '16 at 02:31

1 Answers1

0

The fane.leee is a username. Instagram requires a user id in the API caller.

To get a user id by calling a instagram API function, we could use
https://www.instagram.com/{username}/?__a=1

Another way to get the user id is to use a 3rd party website. It is able to retrieve an Instagram user id from a username. It is here


Reference:
An answer from Thinh Vu in this thread - Not the accepted answer, but the answer underneath. P.S the accepted answer is not working anymore.

Community
  • 1
  • 1
r0n9
  • 2,505
  • 1
  • 29
  • 43