dreamMyDream2014 2015-03-05 23:02
浏览 20
已采纳

twitter api在php中没有使用foreach循环

I cannot get the foreach loop to display any data from the twitter api

Please see code below. However, it does display data on print_r array.

thank you for your time in advance

     ini_set('display_errors', 1);
     require_once('TwitterAPIExchange.php');

 /** Set access tokens here - see: https://dev.twitter.com/apps/ **/
  $settings = array(
      'oauth_access_token' => "*******",
       'oauth_access_token_secret' => "*******",
       'consumer_key' => "********",
       'consumer_secret' => "*******"
      );


       /** Perform a GET request and echo the response **/
       /** Note: Set the GET field BEFORE calling buildOauth(); **/
       $url = 'https://api.twitter.com/1.1/search/tweets.json';
        $getfield = '?q=love+you&count=20';
        $requestMethod = 'GET';
        $twitter = new TwitterAPIExchange($settings);
        $string = json_decode($twitter->setGetfield($getfield)
         ->buildOauth($url, $requestMethod)
         ->performRequest(),$assoc = TRUE);
        if($string["errors"][0]["message"] != "") {echo "<h3>Sorry, there 
          was a problem.</h3><p>Twitter returned the following error 
       message:</p><p><em>".$string[errors][0]["message"]."
       </em></p>";exit();}


    foreach($string as $tweets) {

      echo $tweets['name'] . '<br />';

    }

    cannot get the foreach loop to display any data from the twitter api

Please see code below. However, it does display data on print_r array.

thank you for your time in advance

 /**here is the a sample of the array data but when the foreach loop is 
added the screen is blank no error is dispayed no data is displayed**/

Array
(
[statuses] => Array
(
[0] => Array
(
[metadata] => Array
(
[iso_language_code] => en
[result_type] => recent
)

            [created_at] =&gt; Thu Mar 05 23:16:36 +0000 2015
            [id] =&gt; 573623174589472768
            [id_str] =&gt; 573623174589472768
            [text] =&gt; @TheVampsband #TheVampsVIP Please choose me and 
@Charlotte_94x to win
  • 写回答

1条回答 默认 最新

  • dourao1968 2015-03-05 23:57
    关注

    So first of all you loop over wrong array. Array you looking for is under statuses index, so change your loop into:

    foreach($string['statuses'] as $tweets) {
        echo $tweets['name'] . '<br />';
    }
    

    Look at your print_r() and check carefully how sub array are build. E.g. if you want to print name of each tweet author, you should modify yor echo in foreach to:

    echo $tweets['user']['name'] . '<br />';
    

    because output array has structure

    array(..., 'user' => array('name' => 'Example name', ...));
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里