如何解析json输出?
问题描述:
我试图解析此网址 https://esewa.com.np/epay/transdetails?pid=AddFund-C-11970239- 9625960&amt=100&scd=nprhosting&rid=00C3LF0如何解析json输出?
{
"code":"00",
"msg":"Success",
"txnDetail": {
"txnCode":"00C3LF0",
"amt":"100.0",
"date":"2015-07-16 23:44:18.0",
"payerId":"[email protected]",
"status":"COMPLETE",
"pid":"AddFund-C-11970239-9625960",
"txAmt":"0",
"psc":"0",
"pdc":"0"
}
}
像这样
$fields = array(
'pid' => "AddFund-C-11970239-9625960";
'amt' => "100.0";
'scd' => "nprhosting";
'rid' => "00C3LF0";
);
$field2 = json_encode($fields);
$url = "https://esewa.com.np/epay/transdetails";
// Open connection
$ch = curl_init();
// Set the url, number of POST vars, POST data
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_POSTFIELDS, $field2);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($field2))
);
// Execute post
$result = curl_exec($ch);
// Close connection
curl_close($ch);
//
///Deocde Json
$data = (json_decode($result, true));
var_dump($data);
$message =$data['msg'];
$status =$data['txnDetail']['status'];
echo $message;
echo $status;
仍然没有输出?
答
阵列是不正确,除去:
:
$fields = array(
'pid' => "AddFund-C-11970239-9625960",
'amt' => "100.0",
'scd' => "nprhosting",
'rid' => "00C3LF0"
);
并尝试
$url = "http://examplesite.com/epay/transdetails?" . http_build_query($fields);
夹头的POSTFIELDS
和HTTPHEADER
curl_setopt($ch, CURLOPT_POST, false);
的参数被期望用作GET(按照你提供的链接),保持简单。
另请检查this回答以便更好地了解如何使用PHP CURL发送HTTP GET请求。
+0
请求方法可能是post/get在开发中。在这种情况下指导。我尝试了上面的$ url,但仍然没有结果 –
答
我尝试过了,工作..
$url = 'https://example.com/epay/transdetails?pid=AddFund-C-11970239-9625960&amt=100&scd=nprsite&rid=00C3LF0';
$data = file_get_contents($url);
$arr = json_decode($data,true);
echo $arr['txnDetail']['status'];
print_r($arr);
检查HTTP服务器错误日志文件。这就是php在运行时发布有关错误的信息。 – arkascha
您要添加到URL的数据只是一个查询字符串。你不应该''json_encode()'它。 – 2015-07-19 06:18:36
我尝试过两种方式..仍然有些不好 –