我有一個簡單的 json 代碼來從網站獲取詳細資訊,我的控制器如下所示:
public function trackinsert()
{
$data = array();
$awb = $this->input->post('awb');
if($awb == ""){
echo json_encode(array('error' => true, 'msg' => 'Please enter AWB number'));
exit;
}
$in = array("AwbNumber" =>array($awb));
$post_data = json_encode($in);
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => "https://portal.teamex.ae/webservice/GetTracking",
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 30,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "POST",
CURLOPT_POSTFIELDS => $post_data,
CURLOPT_HTTPHEADER => array( "Content-Type: application/json",
"API-KEY:ff839ddcd02d22d5d7042d457ae04173",
),
));
$response = curl_exec($curl);
$err = curl_error($curl);
curl_close($curl);
$data = base64_encode($response);
echo json_encode(array('error' => false, 'msg' => $response, 'segment' => $data));
$result = base64_decode($data);
$result = json_decode($result);
/ exit;
$this->load->view('teamxtrackresult', array('data' => $result));
}
這給了我以 json 格式顯示的所有資料,但在控制器中,我試圖獲取它特定的變數并將其顯示在如下視圖中不起作用:
<p>AwbNumber: <span id="code"><?php echo $data->awb_number ?></span></p>
它只是給我一個錯誤:
訊息:未定義的屬性:stdClass::$awb_number
誰能告訴我如何使用變數在視圖中顯示值,提前致謝
uj5u.com熱心網友回復:
您可以通過在視圖頁面中列印來檢查回應格式
使用它來列印來自控制器的回應以在 teamxtrackresult.php 中查看插入這一行 print_r($data);
您將獲得來自控制器的總資料以供查看
如果資料進入陣列,則需要使用 $data['awb_number']。如果陣列中有多個值,則可以使用鍵索引進行列印。例如 $data[0]['awb_number']。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/315059.html
標籤:php html json 代码点火器 codeigniter-3
