【发布时间】:2021-11-21 07:36:32
【问题描述】:
我有一个简单的 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
谁能告诉我如何使用变量显示视图中的值,提前谢谢
【问题讨论】:
-
良好的代码缩进将帮助我们阅读代码,更重要的是,它将帮助您调试代码Take a quick look at a coding standard 为您自己的利益。您可能会被要求在几周/几个月内修改此代码,最后您会感谢我的。
-
这是 $this->input->post('awb');你希望展示什么?
-
@GazmendSahiti 它是来自用户的值,当用户提交它时,控制器将使用 json 从网站获取数据并显示结果
-
做一个简单的
print_r($data);看看数据结构是什么样的 -
@RiggsFolly 我收到此错误 stdClass 类的对象无法转换为字符串
标签: php html json codeigniter codeigniter-3