Php 没有收到PUT post数据
我正在使用cURL通过PHP向我的站点发送PUT请求:Php 没有收到PUT post数据,php,post,curl,put,Php,Post,Curl,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,htt
$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;
使用HTTP客户端类帮助发送请求。有几个可用的,但我已经创建了一个()可以帮助您 发出PUT请求:
<?php
require_once 'Http/Client.php';
require_once 'Http/Method.php';
require_once 'Http/PUT.php';
require_once 'Http/Request.php';
require_once 'Http/Response.php';
require_once 'Http/Uri.php';
use Http\Request;
use Http\Response;
header('Content-type:text/plain');
$client = new Http\Client();
//GET request
echo $client->send(
Request::create()
->setMethod(new Http\PUT())
->setUri(new Http\Uri('http://localhost/linetime/user/1'))
->setParameter('a', 'hello')
)->getBody();
?>
//simply print out what was sent:
switch($_SERVER['REQUEST_METHOD']) {
case 'PUT':
echo file_get_contents('php://input');
break;
}
//initialization code goes here
$requestBody = http_build_query(
array('a'=> 'hello'),
'',
'&'
);
$fh = fopen('php://memory', 'rw');
fwrite($fh, $requestBody);
rewind($fh);
curl_setopt($this->curl, CURLOPT_INFILE, $fh);
curl_setopt($this->curl, CURLOPT_INFILESIZE, strlen($requestBody));
curl_setopt($this->curl, CURLOPT_PUT, true);
//send request here
fclose($fh);
注意,我的项目中有一个自动加载程序,它将加载所有这些包含给我的文件,但是如果您不想沿着该路径走下去,您可能需要考虑生成一个包含所有内容的文件。
无库:
<?php
require_once 'Http/Client.php';
require_once 'Http/Method.php';
require_once 'Http/PUT.php';
require_once 'Http/Request.php';
require_once 'Http/Response.php';
require_once 'Http/Uri.php';
use Http\Request;
use Http\Response;
header('Content-type:text/plain');
$client = new Http\Client();
//GET request
echo $client->send(
Request::create()
->setMethod(new Http\PUT())
->setUri(new Http\Uri('http://localhost/linetime/user/1'))
->setParameter('a', 'hello')
)->getBody();
?>
//simply print out what was sent:
switch($_SERVER['REQUEST_METHOD']) {
case 'PUT':
echo file_get_contents('php://input');
break;
}
//initialization code goes here
$requestBody = http_build_query(
array('a'=> 'hello'),
'',
'&'
);
$fh = fopen('php://memory', 'rw');
fwrite($fh, $requestBody);
rewind($fh);
curl_setopt($this->curl, CURLOPT_INFILE, $fh);
curl_setopt($this->curl, CURLOPT_INFILESIZE, strlen($requestBody));
curl_setopt($this->curl, CURLOPT_PUT, true);
//send request here
fclose($fh);
请注意,您使用流发送数据。请确保指定内容长度标题并将post字段设置为字符串
$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);
不,不是。刚刚测试:该代码在我的系统上运行,尽管它因为没有使用
文件\u get\u contents
而显得笨重。这可能是因为我使用的是MAMP吗?curl请求运行得很好,但我仍然没有收到请求另一端的数据。@XcodeDev尝试使用上面更新的代码。我已经验证了它在本地主机上对我自己有效。@XcodeDev那么您没有向PHP发送任何输入。这可能是因为您没有像应该的那样向PHP发送流。发布了一些PHP代码,用于在没有库的情况下执行此操作。您的意思是在web服务器的配置中将脚本指定为PUT处理程序?例如,在Apache中使用指令:Script PUT/your/Script.php
。尝试强制执行内容长度
标题。@XcodeDev请参阅下面我更新的帖子。我认为问题在于你发送数据的方式,而不是你接收数据的方式。谢谢你的帖子,我还是会遇到同样的问题!