0

I've made a request to the instagram API for a list of the users followers. The expected return is this..

{
"data": [{
    "username": "kevin",
    "profile_picture": "http://images.ak.instagram.com/profiles/profile_3_75sq_1325536697.jpg",
    "full_name": "Kevin Systrom",
    "id": "3"
},
{
    "username": "instagram",
    "profile_picture": "http://images.ak.instagram.com/profiles/profile_25025320_75sq_1340929272.jpg",
    "full_name": "Instagram",
    "id": "25025320"
}]

I've tried using this but it isn't working!!

foreach ($followerList['data'] as $follower) {
    echo $follower['full_name'], '<br>';
}

I can't see where I'm going wrong for the life of me, any ideas? Thank you

EDIT: $followerList is already decoded, I'm just struggling to access the array because of the "data" thingy in the expected output. I'm not sure how to access the array through the "data" thingy to then access the elements!

1 Answers1

0

Because that response is in JSON, PHP just sees it as a string. You need to decode it:

$followerList = json_decode($followerList, true);

Then you can access $followerList as an array.

Also, your JSON appears to be missing an ending }.

Enstage
  • 2,106
  • 13
  • 20
  • I already did decode it here in a function function getFollowerList($access_token) { $url = "https://api.instagram.com/v1/users/self/followed-by?access_token=".$access_token.""; $instagraminfo = connectToInstagram($url); $results = json_decode($instagraminfo,true); return $results; } $followerList is the array, I believe the problem is in the foreach :S – WreckTangle May 11 '17 at 01:13
  • Your json is missing an ending `}`, you probably just missed it when copying it here. Trying placing `print_r($followerList);` before the `foreach` loop, to check what is inside it. – Enstage May 11 '17 at 01:18
  • The print_r gives me the following.. Array ( [pagination] => Array ( ) [data] => Array ( ) [meta] => Array ( [code] => 200 ) ) – WreckTangle May 11 '17 at 01:37
  • If `$followerList` is truly the decoded response you are getting from Instagram then you are not getting the expected response from Instagram, you may be using the wrong API or not inputting all parameters. – Enstage May 11 '17 at 01:40
  • Yeah it is! Weird! Other calls work such as getting the username, heres the full thing https://pastebin.com/PM9x64ht – WreckTangle May 11 '17 at 01:51