为foreach()Instagram API提供的参数无效
问题描述:
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'];
?>
答
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...
}