未收到 PUT 发布数据

发布于 2024-12-14 10:19:41 字数 588 浏览 0 评论 0原文

我使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冬天的雪花 2024-12-21 10:19:41

确保指定内容长度标题并将帖子字段设置为字符串

$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); 

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); 
另类 2024-12-21 10:19:41

使用 HTTP 客户端类来帮助发送请求。有几个可用,但我创建了一个(https://github.com/broshizzledizzle/Http-Client< /a>)我可以为您提供帮助。

发出 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();

?>

处理 PUT 请求:

//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);

请注意,您使用流来发送数据。

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:

<?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();

?>

Processing a PUT request:

//simply print out what was sent:
switch($_SERVER['REQUEST_METHOD']) {
    case 'PUT':
        echo file_get_contents('php://input');

        break;
}

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:

//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);

Note that you use a stream to send the data.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文