Can I add data to the body of an HTTP request using cURL in PHP?

I would like to add some data to the body of the HTTP request using cURL in PHP.

Is it possible? How?

I am sending an 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 cannot figure out how to do this.

It is assumed that it looks 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
+3
source share
3 answers

Not 100% sure what you mean ...

/ - , - :

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

...

EDIT:

, POSTFIELDS. POST 1..

. (- - )

$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);
+6

post CURLOPT_POSTFIELDS.

+1

You need to look curland more specifically curl_setopt.

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

0
source

Source: https://habr.com/ru/post/1716559/


All Articles