How to accept XML post request in php? - php

For an android application, data needs to be posted as a xml request to a php file.
How can the XML request be read at the php end.

Use php://input and file_get_contents() to fetch the raw input.
$content = file_get_contents('php://input');

Related

Reading Request Payload in PHP

How can I read content of Request Payload (POST) using PHP?
I´m using a Angular aplication to send the POST.
I've tried with: $_POST, $_FILES and file_get_contents('php://input') ...
None worked.
If you need view x-www-form-urlencoded post data, just use
print_r($_POST);
If you need view json post data, use this
$data=json_decode(file_get_contents('php://input'),1);
print_r($data);
Try:
$payload = file_get_contents('php://input');

How can I create a RESTfull service to retrieve a json or xml file from a client

I have to write a RESTfull service in PHP which can send json data to the caller and retrieve json (or xml) data from the user. I know how to send json or xml data, but not how I can get data back from the user.
The simplest way is getting JSON data as POST or PUT body. To get PHP body:
$entityBody = file_get_contents('php://input');
and then decode json into a PHP object:
$requestBody = json_decode ( $entityBody);
If you are not sour the request body is XML or JSON then check Content-Type in the header.
A RESTful api usually doesnt expect a "response" from the client.
What you want to do is to create an endpoint for the client to POST the specific content.
/api/user/?json={...}
On the PHP side you can retrieve the data with $_REQUEST['json'] (which includes both POST and GET.
There is something called HATEOS that can be used for telling the client about links associated with the current resource (if you want to "chain" calls between the client and the service).

How to send json string to phil sturgeon's Rest Controller?

This question may be stupid. I want to know if its possible to send a string like {"type":"kissme", "who":"john bennet"} to rest controller as a post request.
I know that post data has to be sent as somekey=somevalue&otherkey=othervalue as body of the request. While receiving we refer to the key to get the data. But how can we send any string without assigning to a key like json data ? If it is possible to send, how to receive ?
Thank you,
Abbiya
REST implementation for CodeIgniter
You can use any of the following formats too simply by appending
/format/json to the end of your URL or passing the correct MIME-type:
xml
json
serialize
php (raw output that can be used in eval)
html
csv
Source: http://philsturgeon.co.uk/blog/2009/06/REST-implementation-for-CodeIgniter
i used file_get_contents('php://input'); while reading in controller. i use the content-type header as application/x-www-form-urlencoded while posting the data

Send json as request body or POST json in a variable

I am trying to figure out, is there any way of sending raw json to php instead of POST parameter. If it is possible then which way is best i mean either request body or POST parameter.
Send an HTTP request which has a JSON string as its request body (here: using curl on the command line):
$ curl -d '{"foo":"bar"}' example.com/test.php
Read this request body in PHP:
$json = file_get_contents('php://input');
Decode it:
$data = json_decode($json, true);
PHP's $_POST superglobal is automatically populated as an array if the request body of a POST request contains URL encoded key-value pairs (e.g. foo=bar&baz=42). In the above example you're still using an HTTP POST request with "POST data". It just doesn't automatically end up in $_POST because PHP doesn't know how to decode JSON automagically.

Post XML via xmlhttpresponse and read xml from php

I have managed to send an xml via post using xmlhttprequest. I have also managed to read the whole xml syntax by an aspx page using
Dim reader As System.IO.StreamReader = New System.IO.StreamReader(Page.Request.InputStream)
Dim xmlData As String = ""
xmlData = reader.ReadToEnd()
I am now trying to read the xml from a php page. (I want to read the whole xml, headers and data)
using $_POST I am getting nothing
using file_get_contents("php://input") im getting the xml's data, no headers.
what am I doing wrong? How can I read the whole posted xml?
file_get_contents does the job i want.
althought mozilla displays the pure xml data file_get_contents has

Categories