2015-03-02 59 views
0

我有这个代码为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) 

反正有去,如果这条线失败,则显示出这样的错误消息不会使该网站看起来凌乱?

+1

你得到了什么错误? – 2015-03-02 12:20:35

+0

也许try try catch finally块会帮助你解决这个问题 – igavriil 2015-03-02 12:22:16

+0

'注意:未定义的属性: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

回答

0

foreach仅适用于arrayobjects,其他一切都会生成错误
它不会引发异常:将其包围try/catch块将不起作用。

在你的情况,你必须这样做:

if (is_array ($result->data) || 
    is_object ($result->data)) { 

foreach ($result->data as $photo) { 
    $images[] = array ('url' => $photo->images->{$display_size}->url, 
         'link' => $photo->link); 
    // Note that i have removed a comma after $photo->link 
} 
}else { 
    // can not loop .... 
} 
+0

是得到它的工作,谢谢 – 2015-03-02 13:39:26

0

您可以使用Traversable接口

的可通过接口:接口来检测,如果一个类使用的foreach是穿越。

你的情况

所以,之前的foreach:

if ($result->data instanceof Traversable) { 
    // can use foreach 
}