简体   繁体   中英

file_get_contents does not work in codeigniter

I have a function in my controller to fetch some data from a callback url. And the data will get updated in database automatically.

My callback url is http://example.com/API/sendSMSCallback

and this is my sendSMSCallback function in API controller

public function sendSMSCallback() {
    $json = file_get_contents('php://input'); 
    $data = array('value' => $json, 'date' => date('Y-m-d h:i:s'));
    $this->db->insert('test', $data);    // for testing

    $json = urldecode($json);        
    $obj = json_decode($json,TRUE);
    $reqID = $obj['req_id'];
    $status = $obj['status'];
    $mobile = $obj['msisdn'];

    $this->db->where('UniqueID', $reqID);
    $this->db->where('MobileNo', $mobile);
    $this->db->set('ResponseStatus', $status);
    $this->db->update('smslog_tbl');

    // for testing
    $this->load->helper('file');

    if ( ! write_file(base_url().'test.txt', $json)){
        echo 'Unable to write the file';
    }
    else{
        echo 'File written!';
    }
}

expected data in the form of json as follows

{"result":{"status":"success","req_id":"0d0dd0ce8-dadf-49fd-a49e-9845787e0507","msisdn":"91xxxxxxx, "}}

but nothing will get updated in smslog_tbl table and test.txt is empty. New row is inserted in test table, but value field is empty. Please help me to find out a solution. Thanks in advance

UPDATE : table structure : test

`id` int(11) NOT NULL,
`value` text COLLATE latin1_general_ci NOT NULL,
`date` datetime NOT NULL

First of all HTTP POST data is usually populated in $_POST, php://input will usually contain PUT data.

Also you need to allow

allow_url_fopen

in your php.ini config file. Some hosts disallow it for security

If you already done this then Check file_get_contents PHP Manual return value. If the value is FALSE then it could not read the file. If the value is NULL then the function itself is disabled.

To learn more what might gone wrong with the file_get_contents operation you must enable error reporting and the display of errors to actually read them.

# Enable Error Reporting and Display:
error_reporting(~0);
ini_set('display_errors', 1);

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