sending xml file from php to an android application - php

I am trying to send an xml file as a httpresponse to a post from an android application.
I understood how to send data from a php page here.
Is it possible to send a xml file as a response?
Or do I have to send the content of xml file as a string and parse it in the application?
If it is possible, How do I read the file in my application, so that I can parse it for information?
Thank you.

Send the xml from the server as a string. And in the application side parse the string either using SaxParser, XmlPullParser or DomParser. These are all commonly used.
This is a tutorial from IBM which gives an overview of parsing in android
http://www.ibm.com/developerworks/opensource/library/x-android/index.html

Of course you can. just you can send the xml format type in a php source file.
like following code. sorry for writing [ instead of < or > as this post doesn't allow tag elements.
echo '[?xml version="1.0" encoding="utf-8"?]';
echo '[something][/something]';

Related

How to send xml file for asp.net webservice using php soap

I have to send a XML file to asp.net based webservice. I am using php soap client.
Here is the code:
$request = file_get_contents('myfile.xml');
$result = $soap->__doRequest($request, 'LOCATION_URL', 'ACTION_URL', SOAP_1_2);
AND the xml file looks like:
<ns1:header xmlns:ns1="http://something.com">
<ns1:timestamp>2014-01-01T13:20:00.000-05:00</ns1:timestamp>
But, every time its return a blank response. I didn't get any clue from any side.
Any help will be welcome.

Correct way to POST JSON data from Android to PHP

I have seen many tutorials and questions using the following method to send JSON object to PHP from Android. For example this wordpress blog, this codeproject tutorial and some answers on stackoverflow like these.
All of these tutorials and answers use HTTP header to send data(body) to PHP like this.
....
// Post the data:
httppost.setHeader("json",json.toString());
....
As a programmer we all know that headers are not meant to carry data(body). Headers should only carry metadata.
So, is there a correct way to send JSON data to PHP from Android which does not involve setting data in header?
If you use nativ lib without Volley, here is dummy with HttpClient:
httpClient = createHttpClient();
//You wanna use POST method.
mPost = new HttpPost(_urlStr);
//Head
mPost.addHeader(new BasicHeader("Content-Type", "application/json"));
//Body
((HttpPost) mPost).setEntity(new StringEntity(jsonText));
//Do it.
client.execute(mPost);
Try to use Volley: https://github.com/ogrebgr/android_volley_examples/blob/master/src/com/github/volley_examples/Act_SimpleRequest.java
Here is a simple tutorial to send and receive JSON objects in Android.

slim php (explanation of "request body" documentation)

I am working with Slim PHP for the first time and I am trying to understand one of the concepts. In the slim PHP documentation, it states:
Request Body
Use the request object’s getBody() method to fetch the raw HTTP request body sent by the HTTP client. This is particularly useful for Slim application’s that consume JSON or XML requests.
<?php
$request = $app->request();
$body = $request->getBody();
My question is, what is "the raw HTTP request body"? Is it just a string of all the HTML in the body of the page? What format is it stored as? What would echo $body look like? If I do var_dump($body) I get string(0)"". How do I use it?
I'll just make it an answer rather than comment...
Raw request data is what's submitted from the browser as a body of the POST request.
http://en.wikipedia.org/wiki/POST_%28HTTP%29#Use_for_submitting_web_forms
Technically it can be used to read the data from usual html forms, but this doesn't make much sense as PHP does this good enough and places everything into $_POST.
You may need to read raw data if you have some javascript that sends XML or JSON data, which is not natively accepted by PHP.
The terms you ask for are defined in the RFC2616: Hypertext Transfer Protocol -- HTTP/1.1.
For example, in particular what a Message (Request/Response) Body is: 4.3 Message Body.
If those RFCs are new to you, grab that one an read it from top to bottom and try to understand as much as possible. You'll start to see how those things in the internet work.
Also there is version 2.0 is in the pipe with some changes:
Hypertext Transfer Protocol version 2.0 (Draft 04)
Just in case you're interested.

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

Send XML to php script from flash AS2

I have to send some specific xml to a php script (from flash AS2) that then sends out sms message based on the xml. I have been given the xml by the sms sender and have tested it via their live demo and that works fine. The problem I am having is getting flash to send this XML.
The sms sender states that it needs to recieve the xml in the following format: The XML Document should be posted uriencoded, with a UTF‐8 character set as paramaeter 'xml'
Here is the code I have so far, I think something is missing maybe. I have tried running the swf in a browser rather than in the flash testing environment
var my_xml:XML = new XML('<xml></xml>');
my_xml.contentType = "text/xml";
send_btn.onRelease = function () {
my_xml.send("http://address-to-send-to.com" , "_blank");
};
Any ideas?
Try to use my_xml.sendAndLoad instead of my_xml.send that should return the xml back to your object.
Cheers

Categories