Twitter Search API, tweet IDs, json_decode, & bigint

Today I realized that my Tweet parsing application was saving a bunch of floating numbers as the status ID. After some investigation I realized that Twitter changed their

id

parameter in the Search API response to use a

bigint

. Since our data was returned using JSON, PHP’s native

json_decode

automatically converts the ID to a floating point value, like

1.0034070453002E+15

. Not cool.

If you are finding you’re having this problem when using

json_decode()

in general, and you’re running a development version of PHP 5.3+, you can use the bitmask

JSON_BIGINT_AS_STRING

in your

json_decode()

call to output any bigints as a string.

Example usage:

json_decode($json, false, 512, JSON_BIGINT_AS_STRING);

Anyway Twitter has now added an

id_str

field in the search response. So if you’re using PHP and you need to store or output the status ID, you should use this value instead:

{
    from_user_id_str: "52138619"
    profile_image_url: http://a3.twimg.com/profile_images/403904047/BC_color_logo_normal.jpg
    created_at: "Sat, 13 Nov 2010 03:00:56 +0000"
    from_user: "BCSportsNews"
    id_str: "3281128077139968"
    metadata: {
        result_type: "recent"
    }
    to_user_id: null
    text: "MSOC: North Carolina has defeated Boston College, 1-0, to advance to the ACC Championship game."
    id: 3281128077139968
    from_user_id: 52138619
    geo: null
    iso_language_code: "en"
    to_user_id_str: null
    source: "<a href="http://twitter.com/">web</a>"
}
This entry was posted in Uncategorized. Bookmark the permalink. Post a comment or leave a trackback: Trackback URL.

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>