How to send raw POST data using cURL? (Php)

I am trying to send raw POST data to a page using $ HTTP_RAW_POST_DATA , but my attempts failed and an undefined index notification was indicated.

I tried to do the following:

curl_setopt($handle, CURLOPT_POSTFIELDS, 'Raw POST data'); // Doesn't seem to work at all. curl_setopt($handle, CURLOPT_POSTFIELDS, array('Raw POST data')); // 0 => Raw POST data 

I did some research, and some people suggested sending a header ( Content-Type: text / plain ) in the request, which seemed to not affect anything.

If anyone can provide a solution to this problem, I would really appreciate it. Thanks!

+4
source share
2 answers

For some odd reason, the above heading thing seems to fix it, it didn't work before, I'm not sure why it works now. In any case, for those who do not know what this code is:

 curl_setopt($handle, CURLOPT_HTTPHEADER, array('Content-Type: text/plain')); 
+1
source

You will receive an error message regarding the response of your sender / receiver cycle.

While this may be a sender problem (which does not send the correct request), it may be caused by a misconfiguration in receiving the PHP script. Of course, even if the request is correct, the receiver, however, does not have HTTP_RAW_POST_DATA .

See: http://www.php.net/manual/en/ini.core.php#ini.always-populate-raw-post-data

Always populate $ HTTP_RAW_POST_DATA containing raw POST data. Otherwise, the variable is populated only by an unrecognized MIME data type. However, the preferred way to access the original POST data is php: // input. $ HTTP_RAW_POST_DATA is not available with ENCTYPE = "multipart / form data".

So, the first thing to check is whether the $HTTP_RAW_POST_DATA padding is really what the page above requires:

  • the .ini variable always_populate_raw_post_data is True,
  • or POST is sent with a Content-Type that the POST handler does not recognize (perhaps you can use "text / raw")

At this point, the right way to send data would be

 curl_setopt($handle, CURLOPT_POSTFIELDS, urlencode('Raw POST data')); 

However, note that the recommended way to get this data will not rely on $HTTP_RAW_POST_DATA at $HTTP_RAW_POST_DATA , but rather on reading the contents of the php://input virtual file.

php: // input - a read-only stream that allows you to read the source data from the request authority. In the case of POST requests, it is preferable to use the php: // input instead of $HTTP_RAW_POST_DATA , since it does not depend on special php.ini directives. Moreover, for cases when $ HTTP_RAW_POST_DATA is not populated by default, this is a potentially less energy-intensive alternative to always_populate_raw_post_data activation.

+4
source

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


All Articles