简体   繁体   中英

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

$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.

Is there something like CURLOPT_GETFIELDS or CURLOPT_RAWDATA ? The purpose of sending JSON with GET request is to pass in some params.

I do not wish to add formdata to the request, I wish to post JSON so that it can be parsed on the receiver.

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. How do I achieve this with 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. From RFC 7231 :

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.

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; 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.

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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