I'm trying to use a script that is provided by an E-commerce site that obtains data from an XML feed that is posted to a URL on my site. The script gathers the data using....
$requestBodyXML = new DOMDocument();
# Load the request body into XML and check that the result has been parsed into XML
if ($requestBodyXML->loadXML($HTTP_RAW_POST_DATA) == true)
The problem is that there's no data being passed. I understand this is depreciated, but how else would I accomplish this?
$HTTP_RAW_POST_DATA requires an ini value to be set, using the input stream should work without any special ini settings and is also the 'prefered' method. It is worth noting that neither php://input or $HTTP_RAW_POST_DATA is available with enctype="multipart/form-data".
//The alternative method
$postData = file_get_contents('php://input')
Documentation
Related
I have a central PHP script I use for handling many requests from my page, using XMLHttpRequest() calls from JavaScript, using the GET method. My server PHP is currently 5.6, and yes, I'm avoiding synchronous calls.
It all works well, however there are cases where I'd prefer NOT to have the request remain in browser history, or even be displayed in the URL. So based on the MDN reference on this call, I thought I could simply switch to the POST method, keeping my existing requests (such as "http://myscript.php?cmd=dothis&data=somedata"). then all I'd have to do is add some code to my PHP script, so I could gather the passed data either way...
if ($_SERVER['REQUEST_METHOD'] === 'GET') {
if(isset($_GET['cmd'])) $cmd = $_GET['cmd'];
if(isset($_GET['data'])) $data = $_GET['data'];
}
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
if(isset($_POST['cmd'])) $cmd = $_POST['cmd'];
if(isset($_POST['data'])) $data = $_POST['data'];
}
Well obviously i was dead wrong. What I found was...
Neither of the variables shown in this example are seen by the script within the $_POST array, when I switch to the POST method.
Switching from the $_POST array to the $_REQUEST array works fine (eg: "if(isset($_REQUEST['cmd'])) $cmd = $_REQUEST['cmd']);" ), BUT....
Just the act of issuing my XMLHttpRequest() calls with the POST method, results in errors like this in my log:
PHP Deprecated: Automatically populating $HTTP_RAW_POST_DATA is
deprecated and will be removed in a future version. To avoid this
warning set 'always_populate_raw_post_data' to '-1' in php.ini and use
the php://input stream instead. in Unknown on line 0
So obviously This is a dead end. Doing my best to research what is going on here, in the PHP manual reference to POST I read this right on top...
$_POST $HTTP_POST_VARS [deprecated]
So if I'm understanding this right, the reason I can only obtain the POST variables using the $_REQUEST array is because my requests are still formatted as if I were still using GET, which (apparently?) is a deprecated method. Therefore, "coaxing" it to work is just moving the problem to the future.
Incidentally, I can see by calling the phpinfo.php on my server that despite the ERROR I see in my logs, "always_populate_raw_post_data" is indeed already set to "-1". So all the "solutions" I've found recommending I "JUST" make this setting in my PHP.INI file do not help.
And even if it DID "hide" the error, doing so seems irrelevant to me. Am I wrong in saying that even if I simply gather my passed PHP vars from the $_REQUEST array when using the POST method, AND figure out a way to suppressing the "deprecated" warnings in my LOG, the whole scheme would still fall apart (and make my POST requests fail), the day I finally migrate to PHP 7.x.?
I apologize for asking here, but I have really tried to search every reference I could find for both doing XMLHttpRequests using POST method, AND the deprecated error messages I get in my logs. There is so much obsolete info out there, I can't find any clear discussion on how to PROPERLY alter or re-format my XMLHttpRequests so that I can use the POST method. In fact, I can't even positively determine whether doing so will be possible beyond PHP.5.6. Any help will be very much appreciated.
PHP made a mistake of naming $_GET and $_POST after the default place that a web browser will put data in an HTML form where the method attribute is set to GET or POST.
$_GET gets data from the query string, even if the request method was POST.
The request body is determined by the argument you pass to send. Data you put in the query string remains in the query string.
const url = 'http://myscript.php'
const form_data = new FormData();
form_data.append("cmd", "dothis");
form_data.append("data", "somedata");
cost xhr = new XMLHttpRequest;
xhr.open("POST", url);
xhr.addEventListener('load', function () { console.log(this.response); });
xhr.send(form_data);
I have a simple php file which just echoes $_POST['email']. I want to access it through rest client extension.
New extension looks totally different. This answer uses old extension.
I just added a check isset($_POST['email']). It returns else part data.
How do I post data with new extension?
Here's how to setup the request:
Since you're accessing email in $_POST you'll need to choose the POST method
Set the Content-Type to application/x-www-form-urlencoded
Add the data form entries (in your case email=emailvalue)
Then your PHP script will be able to read the value of $_POST['email']
My environment is PHP 5.5.9, Nginx 1.4.6-1ubuntu3.2, localhost.
I'm trying to get data from PATCH method, but that just return an empty string...
With POST method that's work fine, this is a piece of my script :
case 'POST':
case 'PATCH':
$this->data = file_get_contents("php://input");
$this->data is empty when PATCH method is used and complete when POST, i use the POSTMAN chrome extension and i push RAW data (not multipart/form-data)
I think Nginx was in fault...but nothing into the log file...
Any help will be much appreciated !
I recently ran into a similar issue trying to access php://input for a PUT request. The issue was simply that I had already accessed it once previously (in a logging function that ran prior to the code in question).
POST acts differently than the other methods, which explains the discrepancy:
Note: Prior to PHP 5.6, a stream opened with php://input could only be read once; the stream did not support seek operations. However, depending on the SAPI implementation, it may be possible to open another php://input stream and restart reading. This is only possible if the request body data has been saved. Typically, this is the case for POST requests, but not other request methods, such as PUT or PROPFIND.
source: http://php.net/manual/en/wrappers.php.php
The solution was simple: Store the value of php://input from the initial grab to a PHP variable, and don't try to run file_get_contents("php://input") more than once for a non-POST request.
I have a system that POSTs data to an URL without sending key/value pairs but instead using an XML payload. I need to read that XML data using PHP under Apache, but PHP's $_POST array is for key/value pairs and thus is blank as no keys are provided.
I tried reading from php://input and php://stdin, but that's also blank.
How can I read the raw POST data using PHP? I cannot control the input as it is being generated by a third-party application. Imagine this being a RESTful URL that accepts XML payload.
According to the PHP manual, php://input is not available with enctype="multipart/form-data".. If this is what's being POSTed to you, PHP will not allow you to access the raw data.
One workaround to this issue:
(source: https://stackoverflow.com/questions/1361673/get-raw-post-data)
Add this to your apache.conf:
<Location "/backend/XXX.php">
SetEnvIf Content-Type ^(multipart/form-data)(.*) MULTIPART_CTYPE=$1$2
RequestHeader set Content-Type application/x-httpd-php env=MULTIPART_CTYPE
RequestHeader set X-Real-Content-Type %{MULTIPART_CTYPE}e env=MULTIPART_CTYPE
</Location>
As of PHP 5.4, there is also the "enable-post-data-reading" ini setting (http://php.net/manual/en/ini.core.php#ini.enable-post-data-reading).
From the PHP manual: "Disabling this option causes $_POST and $_FILES not to be populated. The only way to read postdata will then be through the php://input stream wrapper. This can be useful to proxy requests or to process the POST data in a memory efficient fashion."
Try disabling this setting and then reading from php://input.
You can check and see if there is any information stored in your post by doing:
print_r( $_POST );
What you would like to do might be sending the XML data in a key named xml or something like that resulting in $_POST['xml'] = 'your xml code'
EDIT:
It would also be nice if you could post the code you use to create the $_POST.
i want to receive in a server a xml file sent by java by POST
how can e receive that ? and how can e parse the file, in this case a xml file...
regards.
There are numerous tutorials on handling file uploads (including: http://www.php.net/manual/en/features.file-upload.post-method.php ) if you're actually uploading a file. If, instead, you're posting the contents of the file, the contents will be in the $_POST and $_REQUEST arrays - PHP creates and populates these for you.
This is a broad (and basic) question, so a more specific answer will require you to read some tutorials, try some code, and post specific questions about problems you encounter.
If it's a file upload (enctype="multipart/form-data"), use the $_FILES superglobal: Handling File Uploads.
Otherwise, just refer to it by its form field name: $_POST['xmlfile'].
Either way, parse it using the DOM or SimpleXML.
if it's XMLRPC request, you have to use either $HTTP_RAW_POST_DATA or php://input wrapper.