没有收到PUT发布数据 [英] No PUT post data being received

查看:54
本文介绍了没有收到PUT发布数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用cURL通过PHP向我的站点发送PUT请求:

I am sending out a PUT request to my site via PHP using cURL:

$data = array("a" => 'hello');
$ch = curl_init('http://localhost/linetime/user/1');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT");
curl_setopt($ch, CURLOPT_POSTFIELDS,http_build_query($data));

$response = curl_exec($ch);
var_dump($response);

然后我正在侦听此PUT请求,但该请求未收到任何数据。

I am then listening for this PUT request, but receiving no data with the request. Please can you tell me where I am going wrong?

$putData = '';
$fp = fopen('php://input', 'r');
while (!feof($fp)) {
    $s = fread($fp, 64);
    $putData .= $s;
}
fclose($fp);
echo $putData;
exit;


推荐答案

确保指定内容长度的标头并进行设置将字段作为字符串发布

make sure to specify a content-length header and set post fields as a string

$data = array("a" => 'hello');    
$fields = http_build_query($data)
$ch = curl_init('http://localhost/linetime/user/1');

curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "PUT"); 

//important
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Length: ' . strlen($fields))); 

curl_setopt($ch, CURLOPT_POSTFIELDS,$fields); 

这篇关于没有收到PUT发布数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆