未收到 PUT 发布数据
我使用 cURL 通过 PHP 向我的网站发送 PUT 请求:
$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 请求,但没有收到该请求的任何数据。请问你能告诉我哪里出错了吗?
$putData = '';
$fp = fopen('php://input', 'r');
while (!feof($fp)) {
$s = fread($fp, 64);
$putData .= $s;
}
fclose($fp);
echo $putData;
exit;
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);
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;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
确保指定内容长度标题并将帖子字段设置为字符串
make sure to specify a content-length header and set post fields as a string
使用 HTTP 客户端类来帮助发送请求。有几个可用,但我创建了一个(https://github.com/broshizzledizzle/Http-Client< /a>)我可以为您提供帮助。
发出 PUT 请求:
处理 PUT 请求:
请注意,我的项目上有一个自动加载器,它将为我加载所有这些包含内容,但您可能想要如果您不想走这条路,请考虑创建一个包含所有内容的文件。
无库:
请注意,您使用流来发送数据。
Use an HTTP client class to help send the requests. There are several available, but I have created one (https://github.com/broshizzledizzle/Http-Client) that I can give you help with.
Making a PUT request:
Processing a PUT request:
Note that I have an autoloader on my projects that will load all those includes for me, but you may want to consider making a file that will include everything if you don't want to go down that route.
Library-less:
Note that you use a stream to send the data.