【问题标题】:Why a wrong tweet id is returned from twitter API?为什么从 twitter API 返回错误的推文 ID?
【发布时间】:2011-02-19 23:13:05
【问题描述】:

我使用 twitter API 来检索用户主页时间线推文。我使用 json 响应格式。最近推文 id(在 API 中只是“id”)被重新调整错误。举个例子

通常它应该像这样返回:“id”:14057503720,(示例来自推特控制台) 但是根据我的要求,它返回如下:“id”:1172601832

它少了 1 位,完全不同。我需要正确的 ID,因为我无法使用 since_id 或 max_id 等参数。

【问题讨论】:

    标签: json api twitter


    【解决方案1】:

    使用id_str 代替id。它似乎没有记录在案,但如果您查看 JSON 的原始源代码,您会发现每条推文的 id_str 都与推文的 ID 正确对应。

    【讨论】:

      【解决方案2】:

      如何获取 ID 的示例

      $url = "http://search.twitter.com/search.json?q=QUERY"; //<--- replace the word QUERY for your own query 
      $data = get_data($url);
      $obj = json_decode($data);
      
      function get_data($url){
          $ch = curl_init();
          $timeout = 5;
          curl_setopt($ch,CURLOPT_URL,$url);
          curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
          curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,$timeout);
          $data = curl_exec($ch);
          curl_close($ch);
          return $data;
          }
      
      foreach ($obj->results as $item){
          $text = $item->text;
          $user = $item-> from_user;
          $img = $item->profile_image_url;
          $tweetId = $item->id_str;  // <--- On this line we are getting the ID of the tweet
      
      echo ' @';  
      echo $user;
      echo $text;
      echo 'Tweet ID: '. $tweetId; //<-- On this line we display the ID of the tweet
      

      欲了解更多信息GET search | Twitter Developers

      第 30 行的示例请求显示 "id_str":"122032448266698752" 这就是使用$tweetId = $item-&gt;id_str;的原因 获取id_str

      【讨论】:

        【解决方案3】:

        它少了 1 位,完全不同。我需要正确的 ID,因为我无法使用 since_id 或 max_id 等参数。

        这并没有完全不同;只是不同。如果你用十六进制写两个 ID,你会收到

        0x345E47BE8
         0x45E47BE8
        

        Tweet IDs are 64-bit 并且在解析的某处丢失了最重要的 32 位一半。使用id_str 作为其他(也在链接的文章中)建议。

        【讨论】:

          【解决方案4】:

          "[文本格式数字编码的歧义]是处理大数字时的一个问题;例如,大于 2^53 的整数不能在 IEEE 754 双精度浮点数中精确表示,因此这样的数字在使用浮点数的语言(如 JavaScript)解析时会变得不准确。大于 253 的数字示例出现在 Twitter 上,它使用 64 位数字来标识每条推文。Twitter 的 API 返回的 JSON包含两次推文 ID,一次作为 JSON 数字,一次作为十进制字符串,以解决 JavaScript 应用程序无法正确解析数字的事实"

          来自 Martin Kleppmann 的“设计数据密集型应用程序”

          【讨论】:

            猜你喜欢
            • 2019-04-04
            • 1970-01-01
            • 2021-08-12
            • 1970-01-01
            • 1970-01-01
            • 2015-07-05
            • 1970-01-01
            • 2020-08-19
            • 2018-09-23
            相关资源
            最近更新 更多