简体   繁体   English

如何在PHP中使用curl GET发送原始数据?

[英]How to send raw data with curl GET in PHP?

I am developing REST API and while it is easy to set raw JSON data for request in cURL for POST 我正在开发REST API,尽管很容易为cURL中的POST请求设置原始JSON数据

$payload = json_encode(array("user" => $data));

//attach encoded JSON string to the POST fields
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);

I cannot figure out how to send such data with GET requests. 我无法弄清楚如何使用GET请求发送此类数据。

Is there something like CURLOPT_GETFIELDS or CURLOPT_RAWDATA ? 是否有类似CURLOPT_GETFIELDSCURLOPT_RAWDATA东西? The purpose of sending JSON with GET request is to pass in some params. 使用GET请求发送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. 我不希望将formdata添加到请求中,我希望发布JSON,以便可以在接收方上对其进行解析。

Thanks! 谢谢!

EDIT: 编辑:

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"
}

as you can see, GET request here has data and it is parsed and works perfectly by web server. 如您所见,此处的GET请求包含数据,并且已被Web服务器解析并完美运行。 How do I achieve this with cURL? 如何使用cURL做到这一点?

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. GET请求没有主体,这就是整个想法:您只是从服务器中获取某些内容,而不是向其中发布内容。 From RFC 7231 : RFC 7231

A payload within a GET request message has no defined semantics; GET请求消息中的有效负载没有定义的语义。 sending a payload body on a GET request might cause some existing implementations to reject the request. 在GET请求上发送有效内容正文可能会导致某些现有实现拒绝该请求。

In other words, a GET request can have data, but it should not. 换句话说,一个GET请求可以有数据,但没有。 From earlier in the spec , where GET is defined as a safe method: 规范的早期开始 ,其中GET被定义为一种安全方法:

Request methods are considered "safe" if their defined semantics are essentially read-only; 如果请求方法的定义语义本质上是只读的,则将其视为“安全”。 ie, 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. 也就是说,由于将安全方法应用于目标资源,客户端不会请求也不会期望原始服务器上的任何状态更改。

... ...

Of the request methods defined by this specification, the GET, HEAD, OPTIONS, and TRACE methods are defined to be safe. 在本规范定义的请求方法中,GET,HEAD,OPTIONS和TRACE方法被定义为安全的。

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. 如果您确实希望在GET请求中包含JSON(并将其发送到合理实施的服务器资源),那么它唯一可以放在的地方就是URI中作为查询字符串的一部分。 For GET requests I find using file_get_contents to be much easier than dealing with cURL. 对于GET请求,我发现使用file_get_contents比处理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);

If you want to send it to an unreasonably implemented server resource, and violate the spirit of the HTTP RFCs, you could do this: 如果要将其发送到不合理实现的服务器资源,并且违反了HTTP RFC的精神,则可以执行以下操作:

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

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. 如果确定要专门使用cURL进行此操作,则可以将CURLOPT_CUSTOMREQUEST选项设置为“ GET”并将数据与CURLOPT_POSTDATA设置为好运。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM