我有这个代码为Instagram图像检索器,它运作良好,但可以失败相当严重。如果一个Foreach失败你能否做一个其他的陈述?
<table border="0" width="90%" cellspacing="0" cellpadding="0">
<tr>
<td>
<?php
function fetch_data($url){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 20);
$result = curl_exec($ch);
curl_close($ch);
return $result;
}
$access_token = "xxx.xxx.xxx";
$display_size = "standard_resolution";
$number_of_images = 7;
$result = fetch_data("https://api.instagram.com/v1/users/[user]/media/recent/?count={$number_of_images}&access_token={$access_token}");
$result = json_decode($result);
$images = array();
foreach($result->data as $photo)
{
$images[] = array(
'url' => $photo->images->{$display_size}->url,
'link' => $photo->link,
);
}
?>
<a href="<?php echo $images[0]['link']; ?>" target="new"><img src="<?php echo $images[0]['url']; ?>" border="0" height="200" width="200" /></td>
</tr>
<tr>
<td><a href="<?php echo $images[1]['link']; ?>" target="new"><img src="<?php echo $images[1]['url']; ?>" border="0" height="200" width="200" /></a></td>
</tr>
</table>
</td>
<td><a href="<?php echo $images[2]['link']; ?>" target="new"><img src="<?php echo $images[2]['url']; ?>" border="0" height="400" width="400" /></a></td>
<td valign=top>
<table border="0" width="100%" cellspacing="0" cellpadding="0">
<tr>
<td><a href="<?php echo $images[3]['link']; ?>" target="new"><img src="<?php echo $images[3]['url']; ?>" border="0" height="200" width="200" /></a></td>
</tr>
<tr>
<td><a href="<?php echo $images[4]['link']; ?>" target="new"><img src="<?php echo $images[4]['url']; ?>" border="0" height="200" width="200" /></a></td>
</tr>
</table>
</td>
<td valign=top>
<table border="0" width="100%" cellspacing="0" cellpadding="0">
<tr>
<td> <a href="<?php echo $images[5]['link']; ?>" target="new"><img src="<?php echo $images[5]['url']; ?>" border="0" height="200" width="200" /></a></td>
</tr>
<tr>
<td><a href="<?php echo $images[6]['link']; ?>" target="new"><img src="<?php echo $images[6]['url']; ?>" border="0" height="200" width="200" /></a></td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
</td>
</tr>
</table>
所以,当过这个脚本死了,它说,它无法上线57线57是
foreach($result->data as $photo)
反正有去,如果这条线失败,则显示出这样的错误消息不会使该网站看起来凌乱?
你得到了什么错误? – 2015-03-02 12:20:35
也许try try catch finally块会帮助你解决这个问题 – igavriil 2015-03-02 12:22:16
'注意:未定义的属性:stdClass :: $数据在/home/content/90/9753290/html/bottom.php上57行和 'Warning:Invalid参数提供的foreach()在/home/content/90/9753290/html/bottom.php在线57'是我收到的错误消息,当它死了大声笑 – 2015-03-02 12:25:47