I have a web server where there are a few php scripts. These scripts are web service whereby some data is passed and they return values in json format.
For e.g. : http://www.example.com/test.php?name=abcd&id=100
So far passing values in url is the method and its ok if there is short content. However if I have to pass 1 page worth of content/text to it then there must be another way.
Currently my code is:
Dim web_client As Net.WebClient = New Net.WebClient
Dim Info As String
Dim response As IO.Stream = web_client.OpenRead("http://example.com/auth.php?userid=102&password=123")
Dim stream_reader As New IO.StreamReader(response)
Info = stream_reader.ReadToEnd()
' Close the stream reader and its underlying stream.
stream_reader.Close()
response.Close()
print (Info) ' info has output which is in json format
Is the above method ok and secure ? However if I wish to pass large amounts of data or the method is not secure then what should I do ?
Thank you.
I'm setting an API for my server for another developer. I'm currently using Flash AIR to send POST data to my server, and simply extract the variables as in
$command = $_POST['command'].
However, he's not using Flash, and is sending data like so:
https://www.mysite.com POST /api/account.php?command=login HTTP/1.1
Content-Type: application/json
Connection: close
command=login
params {"pass":"12345678","token":"","appID":"theirApp","user":"johnnyb","ver":"2.0","Library_ID":"1"}
My server is returning him an error saying that the 'command' parameter is missing.
What do I need to do my end to extract the $_POST var 'command' from his above data?
I've tried file_get_contents('php://input') and http_get_request_body(), but although they don't error, they don't show anything.
Thanks for your help.
The request claims that it is sending JSON.
Content-Type: application/json
However, this:
command=login
params {"pass":"12345678","token":"","appID":"theirApp","user":"johnnyb","ver":"2.0","Library_ID":"1"}
… is not JSON.
If you get rid of everything before the { then it would be JSON and you should be able to read it with file_get_contents('php://input') (and could then pass it through a decoder.
I've tried file_get_contents('php://input') and http_get_request_body() … they don't show anything.
They should work.
When I print out file_get_contents('php://input') for the comms … I get command=login, yet...
I thought you said you didn't get anything
if(!isset($_POST['command']))
$_POST will only be populated for the two standard HTML form encoding methods. If you are using JSON then it won't be automatically parsed, you have to do it yourself (with valid JSON input (so the additional data would need to be encoded in the JSON text with the rest of the data)), file_get_contents('php://input') and decode_json).
"Content-Type should be www-form-urlencoded" from #Cole (correct answer)
More info here: http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.1
The command parameter needs to be part of the data, and the whole thing should be valid JSON. As is, command=login, it is not valid JSON.
Add it to the params object or make a containing object, like
{
command:'login',
params :{"pass":"12345678","token":"","appID":"theirApp","user":"johnnyb","ver":"2.0","Library_ID":"1"}
}
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.
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');
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]';