为什么从Twitter API返回错误的推文ID?

时间:2022-10-10 07:07:43

I use twitter API to retrieve the user home timeline tweets. I use json response format. Recently the tweet id (in API it is just 'id') are retuned wrong. As an example

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

normally it should return like this: "id": 14057503720, (example is from twitter console) however at my request it is returned like this: "id": 1172601832

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

It is 1 digit less and it is totally different. I need the proper ID because I can't make use of the parameters like since_id or max_id.

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

3 个解决方案

#1


5  

Use id_str instead of id. It doesn't seem to be documented, but if you look at the raw source of the JSON you can see that id_str for each tweet is the one that correctly corresponds to the ID of the tweet.

使用id_str而不是id。它似乎没有记录,但是如果你查看JSON的原始源,你可以看到每条推文的id_str是正确对应于推文ID的那个。

#2


2  

It is 1 digit less and it is totally different. I need the proper ID because I can't make use of the parameters like since_id or max_id.

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

It is not totally different; just different. If you write both IDs in hex, you'll receive

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

0x345E47BE8
 0x45E47BE8

Tweet IDs are 64-bit and somewhere in parsing you lose the most significant 32-bit half of it. Use id_str as other (also in the linked article) suggest.

推文ID是64位的,在解析的某个地方你丢失了最重要的32位一半。使用id_str作为其他(也在链接文章中)建议。

#3


1  

example of how to get the ID

如何获取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

For more information GET search | Twitter Developers

有关更多信息GET搜索| Twitter开发者

The example request on the line 30 shows "id_str":"122032448266698752" and thats the reason of use $tweetId = $item->id_str; to get the id_str

第30行的示例请求显示“id_str”:“122032448266698752”,这就是使用$ tweetId = $ item-> id_str的原因;获取id_str

#1


5  

Use id_str instead of id. It doesn't seem to be documented, but if you look at the raw source of the JSON you can see that id_str for each tweet is the one that correctly corresponds to the ID of the tweet.

使用id_str而不是id。它似乎没有记录,但是如果你查看JSON的原始源,你可以看到每条推文的id_str是正确对应于推文ID的那个。

#2


2  

It is 1 digit less and it is totally different. I need the proper ID because I can't make use of the parameters like since_id or max_id.

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

It is not totally different; just different. If you write both IDs in hex, you'll receive

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

0x345E47BE8
 0x45E47BE8

Tweet IDs are 64-bit and somewhere in parsing you lose the most significant 32-bit half of it. Use id_str as other (also in the linked article) suggest.

推文ID是64位的,在解析的某个地方你丢失了最重要的32位一半。使用id_str作为其他(也在链接文章中)建议。

#3


1  

example of how to get the ID

如何获取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

For more information GET search | Twitter Developers

有关更多信息GET搜索| Twitter开发者

The example request on the line 30 shows "id_str":"122032448266698752" and thats the reason of use $tweetId = $item->id_str; to get the id_str

第30行的示例请求显示“id_str”:“122032448266698752”,这就是使用$ tweetId = $ item-> id_str的原因;获取id_str