我遇到了 JSON和PHP的问题。 通过CURL,我查询网站的API,作为回应,我有JSON。
当我发出命令var_dump (json_decode ($ result));
时,他打印不好。
我哪里错了?
PHP:
<?php
$url = "https://vulners.com/api/v3/audit/audit/?os=redhat&version=7.1&package=redhat-release-server-7.2-9.el7.x86_64";
$cURL = curl_init();
curl_setopt($cURL, CURLOPT_URL, $url);
curl_setopt($cURL, CURLOPT_HTTPGET, true);
curl_setopt($cURL, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Accept: application/json'
));
$result = curl_exec($cURL);
var_dump(json_decode($result));
var_dump(json_decode($result, true));
curl_close($cURL);
?>
JSON:
{ "result": "OK", "data": { "packages": { "redhat-release-server-7.2-9.el7.x86_64": { "RHSA-2017:3108": [ { "package": "redhat-release-server-7.2-9.el7.x86_64", "providedVersion": "0:7.2-9.el7", "bulletinVersion": "7.2-9.el7_2.3", "providedPackage": "redhat-release-server-7.2-9.el7.x86_64", "bulletinPackage": "redhat-release-server-7.2-9.el7_2.3.x86_64.rpm", "operator": "lt", "bulletinID": "RHSA-2017:3108", "cvelist": [], "fix": "sudo yum -y update redhat-release-server" } ], "RHSA-2017:1384": [ { "package": "redhat-release-server-7.2-9.el7.x86_64", "providedVersion": "0:7.2-9.el7", "bulletinVersion": "7.2-9.el7_2.2", "providedPackage": "redhat-release-server-7.2-9.el7.x86_64", "bulletinPackage": "redhat-release-server-7.2-9.el7_2.2.x86_64.rpm", "operator": "lt", "bulletinID": "RHSA-2017:1384", "cvelist": [], "fix": "sudo yum -y update redhat-release-server" } ] } }, "vulnerabilities": [ "RHSA-2017:3108", "RHSA-2017:1384" ], "reasons": [ { "package": "redhat-release-server-7.2-9.el7.x86_64", "providedVersion": "0:7.2-9.el7", "bulletinVersion": "7.2-9.el7_2.3", "providedPackage": "redhat-release-server-7.2-9.el7.x86_64", "bulletinPackage": "redhat-release-server-7.2-9.el7_2.3.x86_64.rpm", "operator": "lt", "bulletinID": "RHSA-2017:3108", "cvelist": [], "fix": "sudo yum -y update redhat-release-server" }, { "package": "redhat-release-server-7.2-9.el7.x86_64", "providedVersion": "0:7.2-9.el7", "bulletinVersion": "7.2-9.el7_2.2", "providedPackage": "redhat-release-server-7.2-9.el7.x86_64", "bulletinPackage": "redhat-release-server-7.2-9.el7_2.2.x86_64.rpm", "operator": "lt", "bulletinID": "RHSA-2017:1384", "cvelist": [], "fix": "sudo yum -y update redhat-release-server" } ], "cvss": { "score": 0.0, "vector": "NONE" }, "cvelist": [], "cumulativeFix": "sudo yum -y update redhat-release-server", "id": "WAIT4O6XKXKD4SF9KIRJFJ2DG0J47R3DI1Y42J661KCJ5D0N9DB7TLB1E36N5OQ34JDB0PG1MP893WHTFL60YGQ92RJ3FC4JX7SLZNTUU5DTD3GLFK5PQJ5T1NYGVIBT" } }int(1) int(1)
非常好:)
object(stdClass)#1 (5) {
["a"] => int(1)
["b"] => int(2)
["c"] => int(3)
["d"] => int(4)
["e"] => int(5)
}
array(5) {
["a"] => int(1)
["b"] => int(2)
["c"] => int(3)
["d"] => int(4)
["e"] => int(5)
}
答案 0 :(得分:0)
int(1)
是curl_exec
命令的实际返回值documentation states:
成功时返回TRUE,失败时返回FALSE。但是,如果设置了CURLOPT_RETURNTRANSFER选项,它将在成功时返回结果,在失败时返回FALSE。
因此,您可以尝试设置
curl_setopt($cURL, CURLOPT_RETURNTRANSFER, true);
因为这应该会成功回复itselft。
然后使用var_dump(json_decode($result, true));
获得漂亮的印刷品。