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

使用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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?