I'm attempting to send raw POST data to a page using $HTTP_RAW_POST_DATA, but my attempts failed and an undefined index notice was given.
I have attempted 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 had suggested sending a header (Content-Type: text/plain) in the request, which didn't seem to affect anything.
Is there a solution for this issue?
You get an error in the response part of your sender/receiver cycle.
While this might very well be a problem of the sender (which does not send a proper request), it may also be caused by a misconfiguration in the receiving PHP script. To wit, even if the request is correct, the receiver might nonetheless not have HTTP_RAW_POST_DATA
available.
See: http://www.php.net/manual/en/ini.core.php#ini.always-populate-raw-post-data
Always populate the $HTTP_RAW_POST_DATA containing the raw POST data. Otherwise, the variable is populated only with unrecognized MIME type of the data. However, the preferred method for accessing the raw 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 $HTTP_RAW_POST_DATA
is indeed populated, which from the page above requires either:
always_populate_raw_post_data
is True,Content-Type
that the POST handler does not recognize (maybe one could use "text/raw")At this point, the correct way of sending data would be
curl_setopt($handle, CURLOPT_POSTFIELDS, urlencode('Raw POST data'));
However, note that the recommended way of receiving those data would be not to rely on $HTTP_RAW_POST_DATA
at all, but to read the contents of the virtual file php://input
.
php://input is a read-only stream that allows you to read raw data from the request body. In the case of POST requests, it is preferable to use php://input instead of
$HTTP_RAW_POST_DATA
as it does not depend on special php.ini directives. Moreover, for those cases where $HTTP_RAW_POST_DATA is not populated by default, it is a potentially less memory intensive alternative to activating always_populate_raw_post_data.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With