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 oracle集群安装出bug
  • ¥15 关于#python#的问题:自动化测试
  • ¥20 问题请教!vue项目关于Nginx配置nonce安全策略的问题
  • ¥15 教务系统账号被盗号如何追溯设备
  • ¥20 delta降尺度方法,未来数据怎么降尺度
  • ¥15 c# 使用NPOI快速将datatable数据导入excel中指定sheet,要求快速高效
  • ¥15 再不同版本的系统上,TCP传输速度不一致
  • ¥15 高德地图2.0 版本点聚合中Marker的位置无法实时更新,如何解决呢?
  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题