SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Guru
    Join Date
    Feb 2008
    Posts
    655
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    A real stumper with json_decode

    I am working with the Twitter API at the moment and I have run into a problem. Every tweet ID is returned as 2147483647 (maximum value of 32-bit integer). I have worked out that the json data is retrieved correctly, but it is being messed up when it is being decoded.

    Here is a very narrowed-down example:
    PHP Code:
    $data "{\"id\":2755218950}";
    echo 
    "<pre>";
    print_r($data);
    echo 
    "\n";
    print_r(json_decode($data1));
    echo 
    "</pre>"
    So this code should echo out $data and then convert it into an array and echo that out.

    This is the output I get from executing this code:

    Code:
    {"id":2755218950}
    
    Array
    (
        [id] => 2147483647
    )
    How is this even possible? Does anybody have an idea what could cause the value of this number to be changed when the string is decoded?

  2. #2
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    It is not enclosed with quotes therefor it is treated as an integer.
    A workaround: (untested)
    PHP Code:
    $json preg_replace'/id":(\d+)/''id":"\1"'$json ); 
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  3. #3
    PHP Guru lampcms.com's Avatar
    Join Date
    Jan 2009
    Posts
    921
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This number 2147483647
    is a very special number.

    Quote from wiki about this number:
    The number 2,147,483,647 is also the maximum value for a 32-bit signed integer in computing. It is therefore the maximum value for variables declared as int in many programming languages running on popular CPUs, and the maximum possible score for many video games. The appearance of the number often reflects an error, overflow condition, or missing value.[7]

  4. #4
    PHP Guru lampcms.com's Avatar
    Join Date
    Jan 2009
    Posts
    921
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    So, this indicates that you have some problem with php, maybe problem with memory, maybe your server is running out of available memory or maybe this is not the only php code you have in your php file?

    When I tried your code in a test file - with no other code, it worked as expected, I got the array [id] => 2755218950

    So, you may have some error in your php file or there is a problem with your server.

  5. #5
    SitePoint Guru
    Join Date
    Feb 2008
    Posts
    655
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by logic_earth View Post
    It is not enclosed with quotes therefor it is treated as an integer.
    A workaround: (untested)
    PHP Code:
    $json preg_replace'/id":(\d+)/''id":"\1"'$json ); 
    This has worked for me, thanks very much. Do you think that Twitter is returning the data in the appropriate format or is PHP handling it wrong? I have been in touch with the person who built the library I am using and he doesn't encounter this problem when he runs my test code.

    Quote Originally Posted by Sharedlog.com View Post
    This number 2147483647
    is a very special number.

    Quote from wiki about this number:
    It was interesting to read about it. Twitter were worried about this number earlier this year as they were approaching so many tweets. They sorted it out and I don;t think the problem is with the data Twitter is returning.

  6. #6
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,398
    Mentioned
    65 Post(s)
    Tagged
    1 Thread(s)
    The number should become a float which can easily handle those larger numbers. Maybe related, or not, but what version of PHP are you using?
    Salathe
    Software Developer and PHP Manual Author.

  7. #7
    SitePoint Guru
    Join Date
    Feb 2008
    Posts
    655
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Salathe View Post
    The number should become a float which can easily handle those larger numbers. Maybe related, or not, but what version of PHP are you using?
    I am using PHP version 5.2.1 with JSON version 1.2.1.

    I am becoming more convinced this is a server problem as some other people are telling me the code runs fine on their server. I will get in touch with my webhost and see what they say.

  8. #8
    PHP Guru lampcms.com's Avatar
    Join Date
    Jan 2009
    Posts
    921
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Just have them reboot the server.

  9. #9
    . shoooo... silver trophy logic_earth's Avatar
    Join Date
    Oct 2005
    Location
    CA
    Posts
    9,013
    Mentioned
    8 Post(s)
    Tagged
    0 Thread(s)
    Use var_dump it will tell if the value is an integer or a float. values higher then the 32-bit limit will become floats. However if one is using the 64-bit version of PHP this number goes much higher.
    Logic without the fatal effects.
    All code snippets are licensed under WTFPL.


  10. #10
    @php.net Salathe's Avatar
    Join Date
    Dec 2004
    Location
    Edinburgh
    Posts
    1,398
    Mentioned
    65 Post(s)
    Tagged
    1 Thread(s)
    I think that's your most likely problem. PHP 5.2.1 is over two years old. If possible, upgrade to a much more recent version (currently 5.2.10 or 5.3.0). A bug, json_encode integer conversion is inconsistent with PHP, was fixed in PHP 5.2.2.
    Salathe
    Software Developer and PHP Manual Author.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •