2012-11-02 66 views
-1

可能重複:
Invalid argument supplied for foreach()錯誤在我的功能 「的foreach()提供無效參數」

所以,我有這樣的功能:

 <?php 
     function get_instagram($user_id=15203338,$count=6,$width=190,$height=190){ 
      $url = 'https://api.instagram.com/v1/users/'.$user_id.'/media/recent/?access_token=13137.f59def8.1a759775695548999504c219ce7b2ecf&count='.$count; 
      // Let's create a cache 
      $cache = './wp-content/themes/multiformeingegno/instagram_json/'.sha1($url).'.json'; 
      if(file_exists($cache) && filemtime($cache) > time() - 1000){ 
       // If a cache file newer than 1000 seconds exist, use that 
       $jsonData = json_decode(file_get_contents($cache)); 
      } else { 
       $jsonData = json_decode((file_get_contents($url))); 
       file_put_contents($cache,json_encode($jsonData)); 
      } 
      $result = '<div id="instagram">'.PHP_EOL; 
      foreach ($jsonData->data as $key=>$value) { 
       $title = (!empty($value->caption->text))?' '.$value->caption->text:'...'; 
       $location = (!empty($value->location->name))?' presso '.$value->location->name:null; 
       $result .= "\t".'<a class="fancybox" data-fancybox-group="gallery" href="'.$value->images->standard_resolution->url.'"><img src="'.$value->images->low_resolution->url.'" alt="'.$value->caption->text.'" width="'.$width.'" height="'.$height.'" /></a> 
       <div style="display: none;">'.htmlentities($title, ENT_QUOTES, "UTF-8").'<br><em style="font-size:11px">Scattata il '.htmlentities(strftime('%e %B %Y alle %R', $value->caption->created_time + 7200)).' '.htmlentities($location).' (<a target="_blank" style="color:darkgrey" rel="nofollow" href="http://maps.google.com/maps?q='.htmlentities($value->location->latitude).',+'.htmlentities($value->location->longitude).'">mappa</a>)</em></div>'.PHP_EOL; 
      } 
      $result .= '</div>'.PHP_EOL; 
      return $result; 
     } 
     echo get_instagram(); 
     ?> 

我m得到了很多這些錯誤:FastCGI在stderr中發送:「PHP消息:PHP警告:爲foreach()提供的無效參數。的問題是

foreach ($jsonData->data as $key=>$value) { 

這是怎麼回事?

在此先感謝你們! :)

+1

錯誤消息是描述性的 - 你認爲數組其實不是。 'var_dump($ jsonData-> data);'所以看看你自己 – zerkms

+0

呃.. var_dump($ jsonData-> data);做? :) 我是一個PHP noob,我實際上覆制了這個功能的部分... – MultiformeIngegno

+0

@MultiformeIngegno它是PHP中最基本的調試形式。它會轉儲任何傳遞給它的內容 - 在這種情況下,'$ jsonData'是某個對象,並且您_hope_它的'data'屬性是一個數組。 'var_dump()'會證明或反駁。 –

回答

1

當您使用json_decode你的對象。如果你想要一個數組,你可以使用第二個參數json_decode這是一個返回對象或數組的切換。 true給出數組。

您需要修改代碼以使用新陣列。

另一件事,也許會有幫助,(不知道,因爲我不知道什麼是在對象做),是把對象轉換爲一個數組(但是這是一個有點黑客;)):

foreach ((array) $jsonData->data as $key=>$value) { 
0

試試你的foreach之前如下:

if(is_array($jsonData->data)){ 
    // Do the for each 
} else { 
    // It wasn't an array so do something else 
    // Like an error message or w/e 
} 

這是有用的,當你的JSON得到什麼並不總是一個數組。

如果我們假設是一個數組,然後使用它僅用於調試,做在別的一個var_dump($jsonData->data);看看你實際上得到

+0

嚴。..我不太瞭解PHP,但這似乎有點骯髒..?它應該做什麼? :) – MultiformeIngegno

+0

以及它檢查值是否是一個數組。如果你確實總是想要得到一個數組,那麼它可能被用於測試。但是,如果你得到的並不總是一個陣列其prolly最好的解決方案 –

+0

好吧,功能似乎正常工作(像以前一樣)。現在我正在等待看看是否仍然有錯誤。 :) – MultiformeIngegno