2015-07-19 151 views
0

我試圖解析此網址 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; 

仍然沒有輸出?

+0

檢查HTTP服務器錯誤日誌文件。這就是php在運行時發佈有關錯誤的信息。 – arkascha

+0

您要添加到URL的數據只是一個查詢字符串。你不應該''json_encode()'它。 – 2015-07-19 06:18:36

+0

我嘗試過兩種方式..仍然有些不好 –

回答

0

陣列是不正確,除去:

$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請求。

+0

請求方法可能是post/get在開發中。在這種情況下指導。我嘗試了上面的$ url,但仍然沒有結果 –

0

我嘗試過了,工作..

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