如前所述,此错误似乎来自 pecl-json-c 的错误版本,由于许可问题,Ubuntu 将其打包为 php5-json 的别名。
感谢firebase/php-jwt项目,我发现的一个解决方法是检查JSON_C_VERSION
由 pecl-json-c 设置的常量,而不是USE_JSON_BIGINT_AS_STRING
. (由于USE_JSON_BIGINT_AS_STRING
已定义,但未实现)。
这是来自JWT 项目的代码:
<?php
if (version_compare(PHP_VERSION, '5.4.0', '>=') && !(defined('JSON_C_VERSION') && PHP_INT_SIZE > 4)) {
/** In PHP >=5.4.0, json_decode() accepts an options parameter, that allows you
* to specify that large ints (like Steam Transaction IDs) should be treated as
* strings, rather than the PHP default behaviour of converting them to floats.
*/
$obj = json_decode($input, false, 512, JSON_BIGINT_AS_STRING);
} else {
/** Not all servers will support that, however, so for older versions we must
* manually detect large ints in the JSON string and quote them (thus converting
*them to strings) before decoding, hence the preg_replace() call.
*/
$max_int_length = strlen((string) PHP_INT_MAX) - 1;
$json_without_bigints = preg_replace('/:\s*(-?\d{'.$max_int_length.',})/', ': "$1"', $input);
$obj = json_decode($json_without_bigints);
}