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 我的代码无法在vc++中运行呀,错误很多
  • ¥50 求一个win系统下运行的可自动抓取arm64架构deb安装包和其依赖包的软件。
  • ¥60 fail to initialize keyboard hotkeys through kernel.0000000000
  • ¥30 ppOCRLabel导出识别结果失败
  • ¥15 Centos7 / PETGEM
  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗