简体   繁体   English

如何忽略PHP注意:json_decode():在Yii中检测到整数溢出?

[英]How do I ignore PHP Notice: json_decode(): integer overflow detected in Yii?

I'm trying to decode a long integer in JSON, but it always crashes and gives an error in Yii. 我正在尝试在JSON中解码一个长整数,但是它总是崩溃并在Yii中给出错误。 I already use the JSON_BIGINT_AS_STRING option. 我已经使用了JSON_BIGINT_AS_STRING选项。 How do I bypass this error or ignore it? 如何绕过此错误或忽略它?

php > var_dump( json_decode('[66933258,"B009GQ034C",281441845828]', false, 512, JSON_BIGINT_AS_STRING));
PHP Notice:  json_decode(): integer overflow detected in php shell code on line 1
array(3) {
  [0]=>
  int(66933258)
  [1]=>
  string(10) "B009GQ034C"
  [2]=>
  string(19) "9223372036854775807"
}

In the app, it gives 在应用程序中,

PHP Notice – yii\\base\\ErrorException json_decode(): integer overflow detected PHP注意事项– yii \\ base \\ ErrorException json_decode():检测到整数溢出

Just tried your code and on my machine it works perfectly, perhaps it has something to do with your php version or so? 刚刚尝试了您的代码,并且在我的机器上它运行完美,也许与您的php版本有关?

The only thing I can think of is (if you don't need the values as a number, but just the values) to use preg_replace to "escape" the numbers as strings first: 我唯一能想到的是(如果您不需要将值作为数字,而只是值) preg_replace使用preg_replace首先将数字作为字符串“转义”:

$json = '[66933258,"B009GQ034C",281441845828]'; 
var_dump(json_decode(preg_replace('/(\W)(\d+)(\W)/', '\\1"\\2"\\3', $json)));

Will yield this: 将产生此:

array (size=3)
  0 => string '66933258' (length=8)
  1 => string 'B009GQ034C' (length=10)
  2 => string '281441845828' (length=12)

Edit: 编辑:

Now that I'm looking a bit closer: You are actually getting a value on the command line. 现在,我看起来更近了:您实际上在命令行上获得了一个值。 So the JSON_BIGINT_AS_STRING is working correctly. 因此, JSON_BIGINT_AS_STRING正常工作。 The problem is that the json_decode() -function seems to generate a notice before it switches to this behavior. 问题在于json_decode() -函数似乎在切换到此行为之前会生成通知。 Yii's error handler captures notices by default and converts them into an exception. Yii的错误处理程序默认情况下会捕获通知,并将其转换为异常。

In this case the solution might be as simple as: 在这种情况下,解决方案可能很简单:

var_dump(@json_decode('[66933258,"B009GQ034C",281441845828]', false, 512, JSON_BIGINT_AS_STRING));

I would normally advise against using the silence operator because it obscures errors, but in this case it beats the alternative (disabling capturing notices and possibly missing other errors) 我通常建议不要使用静默运算符,因为它会掩盖错误,但是在这种情况下,它会优于其他方法(禁用捕获通知并可能丢失其他错误)

Surround with try/catch statement to intercept the exception. 用try / catch语句括起来以拦截异常。 More points for selecting the proper exception to catch, and preventing false positives from similar exceptions. 选择适当的异常来捕获并防止类似异常导致误报的更多点。

You can use json_decode() with depth[512] and option[JSON_BIGINT_AS_STRING] for large integers : 您可以将json_decode()与depth [512]和option [JSON_BIGINT_AS_STRING]结合使用,以获取大整数:

<?php
    $json = '{"number": 12345678901234567890}';

    $jsonArray = json_decode($json, true, 512, JSON_BIGINT_AS_STRING);

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

    print_r($jsonArray);
?>

JSON_BIGINT_AS_STRING that allows casting big integers to string instead of floats which is the default. JSON_BIGINT_AS_STRING允许将大整数转换为字符串,而不是默认的浮点数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM