【问题标题】:file_get_contents does not work in codeigniterfile_get_contents 在 codeigniter 中不起作用
【发布时间】:2018-04-23 06:18:04
【问题描述】:

我的控制器中有一个函数可以从回调 url 中获取一些数据。并且数据将自动在数据库中更新。

我的回调地址是http://example.com/API/sendSMSCallback

这是我在 API 控制器中的 sendSMSCallback 函数

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!';
    }
}

预期的json形式的数据如下

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

smslog_tbl 表中不会更新任何内容,并且 test.txt 为空。 test 表中插入了新行,但 value 字段为空。请帮我找出解决方案。提前致谢

更新: 表结构:测试

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

【问题讨论】:

  • 请测试表结构
  • 在我的问题中更新
  • $json = file_get_contents('php://input'); 之后尝试执行var_dump($json); 并查看是否有任何数据实际出现?

标签: php json codeigniter callback file-get-contents


【解决方案1】:

首先 HTTP POST 数据通常填充在 $_POST 中,php://input 通常会包含 PUT 数据。

还需要允许

allow_url_fopen

在您的 php.ini 配置文件中。一些主机出于安全考虑不允许这样做

如果您已经这样做了,请检查 file_get_contents PHP 手动返回值。如果值为 FALSE,则它无法读取文件。如果值为 NULL,则函数本身被禁用。

要了解更多关于 file_get_contents 操作可能出现的问题,您必须启用错误报告和错误显示以实际读取它们。

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

【讨论】:

  • allow_url_fopen 已打开。我在本地服务器中使用我的一个 php 文件测试了这个 url。有时它可以工作,有时它不会
  • 你确定你是用put还是post发送数据?
  • 我没有发送数据。它是一个回调函数。数据来自其他服务器,他们说它是 post 参数。所以我只是在$this->db->insert('test', $data); 之后评论了代码
  • 我想确定它是怎么来的,这就是为什么我直接将这些数据插入到我的测试表中
  • 所以你可以用 curl 代替
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-08-30
  • 2013-06-30
  • 1970-01-01
  • 1970-01-01
  • 2014-04-26
  • 2014-11-16
  • 2015-03-12
相关资源
最近更新 更多