简体   繁体   中英

How to parse json output?

I tried to parse this url 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"
             }
  }

Like this

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

Still no output ??

Array is incorrect, remove : :

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

and try

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

Chuck the POSTFIELDS and HTTPHEADER

curl_setopt($ch, CURLOPT_POST, false);

The parameters are expected to be as GET(as per the link you have provided), keep it simple.

Also check this answer for better understanding on how to send HTTP GET request with PHP CURL.

I tried it and worked..

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM