RAW POST using cURL in PHP

241,547

Solution 1

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

Solution 2

Implementation with Guzzle library:

use GuzzleHttp\Client;
use GuzzleHttp\RequestOptions;

$httpClient = new Client();

$response = $httpClient->post(
    'https://postman-echo.com/post',
    [
        RequestOptions::BODY => 'POST raw request content',
        RequestOptions::HEADERS => [
            'Content-Type' => 'application/x-www-form-urlencoded',
        ],
    ]
);

echo(
    $response->getBody()->getContents()
);

PHP CURL extension:

$curlHandler = curl_init();

curl_setopt_array($curlHandler, [
    CURLOPT_URL => 'https://postman-echo.com/post',
    CURLOPT_RETURNTRANSFER => true,

    /**
     * Specify POST method
     */
    CURLOPT_POST => true,

    /**
     * Specify request content
     */
    CURLOPT_POSTFIELDS => 'POST raw request content',
]);

$response = curl_exec($curlHandler);

curl_close($curlHandler);

echo($response);

Source code

Share:
241,547

Related videos on Youtube

The Unknown
Author by

The Unknown

Updated on June 25, 2020

Comments

  • The Unknown
    The Unknown about 4 years

    How can I do a RAW POST in PHP using cURL?

    Raw post as in without any encoding, and my data is stored in a string. The data should be formatted like this:

    ... usual HTTP header ...
    Content-Length: 1039
    Content-Type: text/plain
    
    89c5fdataasdhf kajshfd akjshfksa hfdkjsa falkjshfsa
    ajshd fkjsahfd lkjsahflksahfdlkashfhsadkjfsalhfd
    ajshdfhsafiahfiuwhflsf this is just data from a string
    more data kjahfdhsakjfhsalkjfdhalksfd
    

    One option is to manually write the entire HTTP header being sent, but that seems less optimal.

    Anyway, can I just pass options to curl_setopt() that say use POST, use text/plain, and send the raw data from a $variable?

  • Eric Bloch
    Eric Bloch almost 11 years
    will php set the content-length header for you or should you set that as well?
  • James
    James over 10 years
    I cannot get this to work at all. I have a page that I am trying to post raw data to. That page records all raw data it receives into a database table. There are no new rows at all. Do you know if anything has changed here since '09?
  • xryl669
    xryl669 over 10 years
    This works for me, without specifying any HTTP header.
  • shasi kanth
    shasi kanth almost 9 years
    I just realized that body goes here can include any valid json string.
  • Shadocko
    Shadocko over 8 years
    To post RAW files instead of a variable's contents, see this answer: stackoverflow.com/questions/15508850/… This is especially useful for huge files because file_get_contents() won't help.
  • Kaden Yealy
    Kaden Yealy almost 8 years
    There is a 2G limit for this raw post. If you attempt to send file larger than 2G they will be truncated back to 2G. Its a limitation of the string type being loaded.
  • Biasi Wiga
    Biasi Wiga over 4 years
    what is the type of $result as in what do we expect to see when success/failure
  • Craig B
    Craig B almost 4 years
    @BiasiWiga the $result is what ever the page is returning. generally you would hope to see a 200 response code at least to know it went through.
  • Brad
    Brad over 2 years
    is there a way to just write a raw request in its entirety without curl constructing it for us?