get send like this:

$request_params = array('access_token' => $access_token); $get_params = http_build_query($request_params); $result = json_decode(file_get_contents('https://api.instagram.com/v1/users/self/followed-by?'.$get_params)); 

the answer comes like 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 can not parse through foreach, because I write that the object does not exist. I tried this:

 foreach ($result as $key) { echo $key->data->username; } 
  • one
    Iterate the array, i.e. foreach ($result->data as $key) { . - user207618

2 answers 2

Here is the complete code

  $str = '{ "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" }] }'; $result = json_decode($str); foreach($result->data as $key) { var_dump($key->username); } 

    You can unpack json into an associative array by setting the second parameter of the json_decode function to true. Perhaps you will be more comfortable.

     $str = '{ "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" }] }'; $result = json_decode($str, true); foreach($result['data'] as $key) { var_dump($key['username']); }