Reputation: 71
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
normally it should return like this: "id": 14057503720, (example is from twitter console) however at my request it is returned like this: "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.
Upvotes: 7
Views: 3955
Reputation: 61
"[ambiguity around the encoding of numbers in textual formats] is a problem when dealing with large numbers; for example, integers greater than 2^53 cannot be exactly represented in a IEEE 754 double-precision floating-point number, so such numbers become inaccurate when parsed in a language that uses floating-point numbers (such as JavaScript). An example of numbers larger than 253 occurs on Twitter, which uses a 64-bit number to identify each tweet. The JSON returned by Twitter’s API includes tweet IDs twice, once as a JSON number and once as a decimal string, to work around the fact that the numbers are not correctly parsed by JavaScript applications"
From "Designing Data Intensive Applications" by Martin Kleppmann
Upvotes: 3
Reputation: 2040
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.
It is not totally different; just different. If you write both IDs in hex, you'll receive
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.
Upvotes: 2
Reputation: 11
example of how to get the 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
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
Upvotes: 1
Reputation: 111
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.
Upvotes: 11