jublo / codebird-php

Easy access to the Twitter REST API, Direct Messages API, Account Activity API, TON (Object Nest) API and Twitter Ads API — all from one PHP library.
https://www.jublo.net/projects/codebird/php
GNU General Public License v3.0
777 stars 235 forks source link

JSON Decode Problem #223

Closed andrewspode closed 5 years ago

andrewspode commented 6 years ago

Occasionally, responses from Twitter are being mangled, and you will get no results even though it's fetched some.

Tracing this back has found it to be in the _json_decode function.

if (!(defined('JSON_C_VERSION') && PHP_INT_SIZE > 4)) {
      return json_decode($data, $need_array, 512, JSON_BIGINT_AS_STRING);
    }
    $max_int_length = strlen((string) PHP_INT_MAX) - 1;
    $json_without_bigints = preg_replace('/:\s*(-?\d{'.$max_int_length.',})/', ': "$1"', $data);
    $obj = json_decode($json_without_bigints, $need_array);
    return $obj;

Not all PHP installs have support for JSON_BIGINT_AS_STRING, so this is checking and if not - running it through a regex.

The bug is in this regex and doesn't always manifest itself, but has been happening more often for me recently.

https://stackoverflow.com/questions/19520487/json-bigint-as-string-removed-in-php-5-5

This is the thread I believe the offending Regex is from.

Solutions:

  1. Upgrade the PECL module. If anyone has advice on this please post - it wasn't clear which module needs updating and how.

  2. Fix the RegEx. I'm going to post on the other thread to see if anyone can fix it, it's not my strong area.

  3. Edit the code. I'm using this workaround which so far hasn't caused me any problems, so it's at least better than the current code.

if (!(defined('JSON_C_VERSION') && PHP_INT_SIZE > 4)) {
      return json_decode($data, $need_array, 512, JSON_BIGINT_AS_STRING);
    }
    return json_decode($data, $need_array);

If anyone else has insight, it would be to get a fix for this as I'm sure I'm not the only one having erratic behaviour.

mynetx commented 6 years ago

@unclespode Have you found someone who can fix the regex, maybe? We're looking at including a fix into the next release, so …

andrewspode commented 6 years ago

@jublo unfortunately, I am otherwise disposed. My current code base is using my "fix" above and I've yet to encounter a problem.

sololance commented 6 years ago

@jublo @unclespode this happened due to licensing issue with json, more here https://bugs.php.net/bug.php?id=63520 .

Only possible solution without affecting code is to upgrade to php 7 because jsond merged in that version, see here https://wiki.php.net/rfc/jsond

mynetx commented 5 years ago

@unclespode Since Codebird 4.0.0 will require PHP 7.1.0, we’re safe to implement your quick fix for now and watch this.