Php 想要在json中只获取两个对象吗
我试图用下面的代码做一个代码验证程序。我在StackOverflow中查看了答案,但没有得到正确的结果 代码运行良好,但我只想回显两个值 validate.php页面代码如下:Php 想要在json中只获取两个对象吗,php,json,Php,Json,我试图用下面的代码做一个代码验证程序。我在StackOverflow中查看了答案,但没有得到正确的结果 代码运行良好,但我只想回显两个值 validate.php页面代码如下: if(isset($apKey) && isset($code)) { $en = new En(); $Key = $apKey; $Codes = $code; if($Key == null || $Codes == null) { ech
if(isset($apKey) && isset($code)) {
$en = new En();
$Key = $apKey;
$Codes = $code;
if($Key == null || $Codes == null) {
echo json_encode(['data' => 'No Key or code found, Please try again']);
exit;
}
$response = $en->validate($Key,$Codes);
$result = json_decode($response);
$elements = json_encode(['data' => $result]);
echo $elements;
exit;
}
echo json_encode(['data' => 'No Key or code found, Please try again']);
exit;
现在,我尝试使用以下代码检查此验证页面结果:
$siteurl = urlencode($_SERVER['SERVER_NAME']);
$arrContextOptions = array(
"ssl" => array(
"verify_peer" => false,
"verify_peer_name" => false
)
);
$file = file_get_contents('http://www.validates.com/validate.php?code=' . $check , false, stream_context_create($arrContextOptions));
$checks = json_decode($file, true);
$elements = $checks['data'];
echo print_r($elements);
结果是这样的:
Array
(
[buyer] => abcd
)
$checks = json_decode($file, true);
$data = $checks['data'];
if (array_key_exists("error", $data))
echo $data["description"];
else
echo $data["buyer"];
所以我想做什么。我只想在代码后面回显买家和错误消息:$checks=json\u decode($file,true)代码>
错误结果如下:
{"data":{"error":404,"description":"No sale belonging to the current user found with that code"}}
例如:
if(buyer){echo 'true';}
if(error message){echo 'error';}
我想你可以检查一下是否有错误。您可以这样做:
Array
(
[buyer] => abcd
)
$checks = json_decode($file, true);
$data = $checks['data'];
if (array_key_exists("error", $data))
echo $data["description"];
else
echo $data["buyer"];
不要链接到你的站点并要求我们去那里检查控制台,而是将所有信息放在问题本身中(作为文本,而不是图像)。如果没有,当这些链接发生变化时,这个问题对未来的访问者将毫无用处。@MagnusEriksson您是对的,但控制台代码太长。如果您只包含相关部分,可能不会。请阅读:@MagnusEriksson我已经编辑了我的问题,谢谢。