Home > database >  displaying variables using json in php codeigniter not working
displaying variables using json in php codeigniter not working

Time:09-29

i have a simple json code to fetch details from a website and my controller is like below:

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

this is giving me all the data being displayed in json format but as in controller i tried to get it specific variable and display it in view like below is not working:

<p>AwbNumber: <span id="code"><?php echo $data->awb_number ?></span></p>

its just giving me an error:

Message: Undefined property: stdClass::$awb_number

can anyone please tell me how to display the values in view using variables, thanks in advance

CodePudding user response:

You can check for response format by printing in view page

use this to print the response coming from controller to view in teamxtrackresult.php insert this line print_r($data);

you will get the total data coming from controller to view

if data is coming in array, you need to use $data['awb_number']. if it has multiple values in array you can use key index to print. for example $data[0]['awb_number'].

  • Related