如何在PHP中使用curl GET发送原始数据? [英] How to send raw data with curl GET in PHP?
问题描述
我正在开发REST API,尽管很容易为cURL中的POST请求设置原始JSON数据
I am developing REST API and while it is easy to set raw JSON data for request in cURL for POST
$payload = json_encode(array("user" => $data));
//attach encoded JSON string to the POST fields
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
我不知道如何通过GET请求发送此类数据.
I cannot figure out how to send such data with GET requests.
是否有类似CURLOPT_GETFIELDS
或CURLOPT_RAWDATA
的东西?通过GET请求发送JSON的目的是传递一些参数.
Is there something like CURLOPT_GETFIELDS
or CURLOPT_RAWDATA
? The purpose of sending JSON with GET request is to pass in some params.
我不希望将formdata添加到请求中,我希望发布JSON,以便可以在接收方上对其进行解析.
I do not wish to add formdata to the request, I wish to post JSON so that it can be parsed on the receiver.
谢谢!
基于注释,我想避免造成混淆,因此生成的请求应如下所示:
based on comments I want to avoid confusion, so the resulting request should look like:
GET / HTTP/1.1
Host: 127.0.0.1:3000
Content-Type: application/json
Accept: application/json
Host: 127.0.0.1:3000
content-length: 13
Connection: keep-alive
cache-control: no-cache
{
"a": "b"
}
如您所见,这里的GET请求包含数据,它已被解析并由Web服务器完美地工作.如何使用cURL做到这一点?
as you can see, GET request here has data and it is parsed and works perfectly by web server. How do I achieve this with cURL?
推荐答案
GET请求没有主体,这就是整个想法:您只是从服务器获取内容,而不是向其发布内容.来自 RFC 7231 :
GET requests do not have a body, that's the whole idea: you're just getting something from the server, as opposed to posting something to it. From RFC 7231:
GET请求消息中的有效载荷没有定义的语义; 在GET请求上发送有效内容正文可能会导致一些现有内容 拒绝请求的实现.
A payload within a GET request message has no defined semantics; sending a payload body on a GET request might cause some existing implementations to reject the request.
换句话说,一个GET请求可以有数据,但没有.摘自规范的早期版本,其中GET被定义为一种安全方法:
In other words, a GET request can have data, but it should not. From earlier in the spec, where GET is defined as a safe method:
如果请求方法定义的语义是,则认为它们是安全的" 本质上是只读的;即客户不要求,而是要求 没想到,由于以下原因,原始服务器上的任何状态更改 将安全方法应用于目标资源.
Request methods are considered "safe" if their defined semantics are essentially read-only; i.e., the client does not request, and does not expect, any state change on the origin server as a result of applying a safe method to a target resource.
...
在此规范定义的请求方法中,GET,HEAD, OPTIONS和TRACE方法被定义为安全的.
Of the request methods defined by this specification, the GET, HEAD, OPTIONS, and TRACE methods are defined to be safe.
如果您确实希望在GET请求中包含JSON(并将其发送到合理实施的服务器资源),那么它唯一可以放在的地方就是URI中作为查询字符串的一部分.对于GET请求,我发现使用 file_get_contents
来比处理cURL容易得多.
If you really want to have JSON in your GET request (and send it to a reasonably implemented server resource) the only place it can go is in the URI as part of the query string. For GET requests I find using file_get_contents
to be much easier than dealing with cURL.
<?php
$payload = json_encode(["user" => $data]);
$url_data = http_build_query([
"json" => $payload
]);
$url = "https://some.example/endpoint.php?" . $url_data;
$result = file_get_contents($url);
如果要将其发送到不合理实现的服务器资源,并且违反了HTTP RFC的精神,则可以执行以下操作:
If you want to send it to an unreasonably implemented server resource, and violate the spirit of the HTTP RFCs, you could do this:
<?php
$url = "https://some.example/endpoint.php";
$payload = json_encode(["user" => $data]);
$ctx = stream_context_create(["http" => [
"header"=>"Content-Type: application/json",
"content"=>$payload
]]);
$result = file_get_contents($url, false, $ctx);
如果确定要专门使用cURL进行此操作,则可能很幸运,将CURLOPT_CUSTOMREQUEST
选项设置为"GET",而将CURLOPT_POSTDATA
设置为数据.
If you're determined to do this specifically with cURL, you might have luck with the CURLOPT_CUSTOMREQUEST
option set to "GET" and CURLOPT_POSTDATA
with your data.
这篇关于如何在PHP中使用curl GET发送原始数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!