[英]json_decode Preservation of Type
我正在使用json_decode函数进行解码(并验证来自付款处理器的回发)。 收到的json对象如下
{
"notification":{
"version":6.0,
"attemptCount":0,
"role":"VENDOR",
.....
"lineItems":[
{
"itemNo":"1",
"productTitle":"A passed in title",
"shippable":false,
"recurring":false,
"customerProductAmount":1.00,
"customerTaxAmount":0.00
}
]
},
"verification":"9F6E504D"
}
验证的工作方式如下:一个作为通知节点并附加一个密钥。 此字符串的SHA1哈希的前八个字符应与验证节点的内容匹配。
但是,我注意到在使用json_decode时,双精度值6.0、0.00等被截断为整数(6、0等)。 这会弄乱字符串(就其而言,它不会生成正确的SHA1-hash)。 请注意,由于我需要支持PHP 5.0,因此不能使用深度限制来防止对通知分支进行解码。 我该如何解决这个问题。 我编写的(缺陷)验证代码是:
public function IPN_Check(){
$o = (json_decode($this->test_ipn));
$validation_string = json_encode($o->notification);
}
我尝试了以下方法:
<?php
var_dump(json_decode('
{
"notification":{
"version":6.0,
"attemptCount":0
}
}
'));
并得到以下输出:
object(stdClass)#1 (1) {
["notification"]=>
object(stdClass)#2 (2) {
["version"]=>
float(6)
["attemptCount"]=>
int(0)
}
}
PHP确实在float和int之间有所不同,也许您可以执行gettype($o->notification[$i]) == 'float'
来检查是否需要使用字符串添加零。
UPD。 PHP确实在float和int之间有所作为,但json_encode()
-可能没有。 可以肯定的是,您对所有值都进行了编码-将json_encode()
与JSON_PRESERVE_ZERO_FRACTION
参数一起使用,所有的float / double类型都将正确保存。
看起来ClickBank似乎总是以相同的格式发送,只有两个顶级字段“ notification”和“ verification”。 因此,您可以使用substr
从原始JSON中删除前16个字符( {"notification":
substr
和后27个字符( ,"verification":"XXXXXXXX"}
),然后从那里开始:
$notification = substr($json, 16, -27);
$verification = strtoupper( substr( hash('sha1', $notification . $secret), 0, 8) );
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.