drb56625 2013-06-13 07:40
浏览 64
已采纳

使用php for API 1.1读取Twitter JSON数据

I had to make a mad scramble to get all my Twitter feeds going again when API 1.0 was closed. It took a while but I managed to get the GET statuses/user_timeline working again. Now I'm attempting to use the search and I'm not being able to read the results. The JSON file is returning the correct data.

Here is my PHP code:

$code = $tmhOAuth->request('GET', $tmhOAuth->url('1.1/search/tweets'), array(
     'q' => '%23'.$tagzname,
     'since_id' => $num,
     'lang' => 'en',
     'count' => '2'));

 $response = $tmhOAuth->response['response'];

 var_dump(json_decode($response, true));
 echo "<br><br><br><br>";

 $json_output = json_decode($response, true);

 foreach($json_output as $tweets) {

   // $tmsgid   = $tweets['statuses']['metadata']['id_str'];
   $tmsgid   = $tweets['statuses']['id_str'];
   // $tmsgid   = $tweets['metadata']['id_str'];
   // $tmsgid   = $tweets['id_str'];
   echo 'Msg ID:' . $tmsgid . '<br>';

I just can't get the Msg ID output. What am I missing?

The JSON data looks like this:

array(2) {
  ["statuses"]=>
  array(2) {
    [0]=>
    array(23) {
      ["metadata"]=>
      array(2) {
        ["result_type"]=>
        string(6) "recent"
        ["iso_language_code"]=>
        string(2) "en"
      }
      ["created_at"]=>
      string(30) "Thu Jun 13 07:19:30 +0000 2013"
      ["id"]=>
      float(3.4507795373608E+17)
      ["id_str"]=>
      string(18) "345077953736081409"
      ...
  • 写回答

1条回答 默认 最新

  • doulu4534 2013-06-13 09:00
    关注

    Inside of the statuses is an array with two entries. So you have to define which one you want to show.

    foreach($json_output as $tweets) {
        $tmsgid = $tweets['statuses'][0]['id_str'];
    }
    

    EDIT: The first one was wrong!

    foreach($json_output['statuses'] as $tweets) {
        $tmsgid = $tweets['id_str'];
    }
    

    This should be the correct way to loop through your statuses. I don't know what the second element in your root-array is.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突
  • ¥15 超声波模块测距控制点灯,灯的闪烁很不稳定,经过调试发现测的距离偏大
  • ¥15 import arcpy出现importing _arcgisscripting 找不到相关程序
  • ¥15 onvif+openssl,vs2022编译openssl64
  • ¥15 iOS 自定义输入法-第三方输入法
  • ¥15 很想要一个很好的答案或提示
  • ¥15 扫描项目中发现AndroidOS.Agent、Android/SmsThief.LI!tr