RAW POST using cURL in PHP
I just found the solution, kind of answering to my own question in case anyone else stumbles upon it.
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://url/url/url" );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_POSTFIELDS, "body goes here" );
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));
$result = curl_exec($ch);
PHP CURL Using POST Raw JSON Data
If you wanna use Content-type: application/json
and raw
data, seem your data should be in json
format
$ch = curl_init();
$headers = [
'x-api-key: XXXXXX',
'Content-Type: text/plain'
];
$postData = [
'data1' => 'value1',
'data2' => 'value2'
];
curl_setopt($ch, CURLOPT_URL,"XXXXXX");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postData));
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec ($ch);
$statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
How can I send raw POST data with cURL? (PHP)
For some odd reason the aforementioned header thing seems to fix it. Previously it wasn't working, and I'm unsure why it works now.
Anyway, for those who don't know, this is the code:
curl_setopt($handle, CURLOPT_HTTPHEADER, array('Content-Type: text/plain'));
How to send raw data with curl GET in PHP?
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; 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.
...
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.
How to post raw body data with curl?
curl's --data
will by default send Content-Type: application/x-www-form-urlencoded
in the request header. However, when using Postman's raw
body mode, Postman sends Content-Type: text/plain
in the request header.
So to achieve the same thing as Postman, specify -H "Content-Type: text/plain"
for curl:
curl -X POST -H "Content-Type: text/plain" --data "this is raw data" http://78.41.xx.xx:7778/
Note that if you want to watch the full request sent by Postman, you can enable debugging for packed app. Check this link for all instructions. Then you can inspect the app (right-click in Postman) and view all requests sent from Postman in the network
tab :
RAW data POST API using curl not working but executing in POSTMAN is okay
To make cURL follow a redirect just add this line:
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
for more info see php.net curl
Related Topics
PHP to Search Within Txt File and Echo the Whole Line
How to Remove Array Element and Then Re-index Array
PHP Error: Cannot Modify Header Information - Headers Already Sent
Best Way to Do Multiple Constructors in PHP
In PHP (≫= 5.0), Is Passing by Reference Faster
Magento - Quote/Order Product Item Attribute Based on User Input
Create or Write/Append in Text File
How to Pass Data from JavaScript to PHP and Vice Versa
Trying to Get Property of Non-Object - Laravel 5
PHP: How to Remove Specific Element from an Array
How to Get the Value of an Attribute from Xml File in PHP
How to Call a MySQL Stored Procedure from Within PHP Code
Remove the Last Character from a String
How to Replace Different Newline Styles in PHP the Smartest Way