【问题标题】:How to parse json output?如何解析json输出?
【发布时间】:2015-07-19 06:11:00
【问题描述】:

我试图解析这个网址 https://esewa.com.np/epay/transdetails?pid=AddFund-C-11970239- 9625960&amt=100&scd=nprhosting&rid=00C3LF0

{
"code":"00",
"msg":"Success",
"txnDetail": {
                 "txnCode":"00C3LF0",
                 "amt":"100.0",
                 "date":"2015-07-16 23:44:18.0",
                 "payerId":"dipsnwc@gmail.com",
                 "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;

还是没有输出??

【问题讨论】:

  • 检查您的 http 服务器错误日志文件。那是 php 在运行时发布错误信息的地方。
  • 您添加到 URL 的数据只是一个查询字符串。你不应该json_encode()它。
  • 我尝试了两种方式.. 还是有问题

标签: php json parsing


【解决方案1】:

我试过了..

$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);

【讨论】:

    【解决方案2】:

    数组不正确,删除:

    $fields = array(
    'pid' => "AddFund-C-11970239-9625960",
    'amt' => "100.0",
    'scd' => "nprhosting",
    'rid' => "00C3LF0"
    );
    

    试试

    $url = "http://examplesite.com/epay/transdetails?" . http_build_query($fields);
    

    查一下POSTFIELDSHTTPHEADER

    curl_setopt($ch, CURLOPT_POST, false);
    

    参数应为 GET(根据您提供的链接),保持简单。

    还可以查看this 答案以更好地了解如何使用 PHP CURL 发送 HTTP GET 请求。

    【讨论】:

    • 请求方法可能是 post/get ,就像在 dev 中一样。在这种情况下进行指导。我尝试了上面的 $url 但仍然没有结果
    猜你喜欢
    • 2023-01-11
    • 2018-01-30
    • 2013-04-01
    • 1970-01-01
    • 1970-01-01
    • 2015-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多