Question

I'm using this sample code to call my person Instagram feed. Everything works the second time, but on the initial call I get a Invalid argument supplied for foreach(). Can't figure out why it only works part of the time...

The PHP code:

<?php
    foreach ($response['data'] as $data) {
        $link = $data['link'];
        $id = $data['id'];
        $caption = $data['caption']['text'];
        $author = $data['caption']['from']['username'];
        $thumbnail = $data['images']['low_resolution']['url'];
        $username = $data['user']['username'];
        $fullname = $data['user']['full_name'];
?>
Was it helpful?

Solution

Sometimes for some reasons api may not return response as you expected. So you must validate data before using it.

if(isset($response['data']) && is_array($response['data'])){
    //your code here...
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top