tags:

views:

420

answers:

3

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
A: 

You'll want to look at curl and more specifically curl_setopt.

If you can be more exact about your needs, I can provide an example for you, although the examples on the manual page are quite good to get you started also.

nikc
+1  A: 

You can only add the post values when sending a post request with the CURLOPT_POSTFIELDS option.

Zed
curl is a complete library, of course it is possible to do anything on the data, being the protocol supported.
AlberT
@AlberT care to post an answer then ? :)
Zed
@Zed, already up voted the answer of goddva no need to repeat a good answer again :)
AlberT
@AlberT, I see how this works... he gives a completely wrong answer, I give a correct one. You upvote his and add some invalid comments to mine. Then he looks at my answer, realizes that is the correct one, copies it to his answer, and it is accepted. To top it all, now your comment put on my answer becomes valid for his answer as well, though I do not see it anywhere.
Zed
@Zed, I did _not_ copy your answer.. I have been using php and curl for quite some time. I do see that you did have the correct answer, something I did not see when I updated my answer.. +1
goddva
@goddva, don't take it offensive. I was just being grumpy a bit =)(still don't think AlberT's comment is valid)
Zed
@Zed, maybe I misunderstood the answer (adding headers to the body? lol) .. so plz edit your answer I'd like to upvote yours too, really!
AlberT
+4  A: 

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);
goddva
Nice demonstration :) +1
AlberT
So where is the data added to the http request ?
Zed
Yep, the edit solved it. Thanks a lot :)
nohj