【问题标题】:PHP Post Request SendingPHP Post 请求发送
【发布时间】:2017-07-13 21:44:35
【问题描述】:

我使用 PHP5.3 和 CodeIgniter 框架,也使用 TOAST - CodeIgniter 单元测试库。

我需要测试我的控制器,它需要 POST 来传递变量。我尝试了 cUrl 和 stream_context_create,都失败了。 (卷曲已启用)但是我可以使用具有相同正文数据的 POSTMAN 成功地做到这一点。所以,接收者工作正常,我的发送者有问题。

curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, 'http://localhost:8080/myApp/index.php?/systemAdministration/runScheduledETL');
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl_handle, CURLOPT_POST, 1);
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, array('id' => $id));

$buffer = curl_exec($curl_handle);
curl_close($curl_handle);

另外,我试过了:

url = 'http://localhost:8080/myApp/index.php?/systemAdministration/runScheduledETL';
$data = array('id' => $id);

$options = array(
    'http' => array(
       'header'  => "Content-type: application/x-www-form-urlencoded",
       'method'  => 'POST',
       'content' => http_build_query($data)
    )
);
$context  = stream_context_create($options);
$result = file_get_contents($url, false, $context);

【问题讨论】:

  • 也看到了这个。

标签: php codeigniter curl post


【解决方案1】:

在不知道您遇到的错误的情况下,诊断起来有点困难,但很可能是您的请求被重定向(302 或 301 状态代码)。那会删除帖子数据。

如果请求不以斜杠结尾,我的 .htaccess 会重定向请求,所以不妨尝试在 URL 末尾添加斜杠?

【讨论】:

  • 感谢回复,添加的斜线字符仍然无法发送。
猜你喜欢
  • 1970-01-01
  • 2021-11-30
  • 2014-11-06
  • 2013-07-11
  • 1970-01-01
  • 2011-08-04
  • 2017-05-31
  • 2021-01-09
相关资源
最近更新 更多