Possible to add data to the body of a HTTP request using cURL in PHP?

curl, http, php

Solution

Not 100% sure what you mean...

If you want to grab a page, and replace the content/insert some content to it - you could do something like this:

$ch = curl_init("http://stackoverflow.com/questions/1361169/possible-to-add-data-to-the-body-of-a-http-request-using-curl-in-php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, true);
$output = curl_exec($ch);

$output = str_replace('Possible to add data to the body of a HTTP request using cURL in PHP?', 'I have just changed the title of your post...', $output);

echo $output;

This would print this page...

EDIT:

With the new information added, I do think you should be able to use the POSTFIELDS.. Just remember to set the POST to 1..

E.g. (something like this - not tested)

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://stackoverflow.com");
curl_setopt($ch, CURLOPT_USERAGENT, "My user agent"); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $myOtherHeaderStringVariable); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "I want to add data to the request here");
$output = curl_exec($ch);

Problem

I would like to add some data to the body of a http request using cURL in PHP. Is this possible? How? I'm sending a HTTP request to a remote server. I have added all the headers I want, but now I need to add some more data to the HTTP body but I cant figure out how to do it. It is supposed to look something like this: ``` Host: stackoverflow.com Authorization: Basic asd3sd6878sdf6svg87fS User-Agent: My user agent ... other headers... I want to add data to the request here ```

Original source