如何修复警告JSON_BIGINT_AS_STRING未实现?

时间:2016-08-03 10:08:02

标签: php json

查看我的Apache错误日志文件,我检查了这个警告:

PHP Warning:  json_decode(): option JSON_BIGINT_AS_STRING not implemented in /../codebird.php on line 2517

它是指我用来在博客上自动发布的脚本。

这是罪恶的功能:

  protected function _parseApiReply($reply)
  {
    $need_array = $this->_return_format === CODEBIRD_RETURNFORMAT_ARRAY;
    if ($reply === '[]') {
      switch ($this->_return_format) {
        case CODEBIRD_RETURNFORMAT_ARRAY:
          return [];
        case CODEBIRD_RETURNFORMAT_JSON:
          return '{}';
        case CODEBIRD_RETURNFORMAT_OBJECT:
          return new \stdClass;
      }
    }
    if (! $parsed = json_decode($reply, $need_array, 512, JSON_BIGINT_AS_STRING)) {
      if ($reply) {
        // assume query format
        $reply = explode('&', $reply);
        foreach ($reply as $element) {
          if (stristr($element, '=')) {
            list($key, $value) = explode('=', $element, 2);
            $parsed[$key] = $value;
          } else {
            $parsed['message'] = $element;
          }
        }
      }
      $reply = json_encode($parsed);
    }
    switch ($this->_return_format) {
      case CODEBIRD_RETURNFORMAT_ARRAY:
        return $parsed;
      case CODEBIRD_RETURNFORMAT_JSON:
        return $reply;
      case CODEBIRD_RETURNFORMAT_OBJECT:
        return (object) $parsed;
    }
    return $parsed;
  }
}

为什么标题太长我收到此警告并且不会在Twitter上发布?

P.S。

我已经安装了PHP 5.5.9,但问题仍然存在。

1 个答案:

答案 0 :(得分:1)

自PHP 5.4起,JSON_BIGINT_AS_STRING option才可用。您可以将其删除,但如果JSON响应中包含的数字太大,则会溢出。

进一步研究,似乎有一些problems with JSON's license导致基于Debian的发行版不提供标准JSON扩展的包。他们用一个主要兼容的版本替换它,该版本定义了一个可以检查的常量JSON_C_VERSION

if (defined("JSON_C_VERSION") || version_compare(PHP_VERSION, '5.4.0', '<')) {
    json_decode($reply, $need_array, 512);
} else {
    json_decode($reply, $need_array, 512, JSON_BIGINT_AS_STRING);
}

或者,只需删除第四个参数。我一直使用的是作为RHEL发行版的Scientific Linux,所以之前从未遇到过这个问题。

相关问题