views:

962

answers:

5

And from the client side, which request type (GET or POST) is better to use, to send JSON data if I use XmlHTTPRequest? My application use this stream of data for either retrive data form database and execute some functionality in PHP.

EDIT:

My question was inspired from this answer: http://stackoverflow.com/questions/813487/how-to-post-json-to-php-with-curl/813512#813512

He says:

From a protocol perspective file_get_contents("php://input") is actually more correct, since you're not really processing http multipart form data anyway.

A: 

I don't understand your title question (What do you mean by a JSON request? A request to fetch JSON)?

but your second question is easy, GET has a size limitation of 1-2 kilobytes on both the server and browser side, so any kind of larger amounts of data you'd have to send through POST.

Pekka
I mean a request that contains a JSON formatted data, from client to server.
Manuel Bitto
A: 

The usual rules should apply for how you send the request. If the request is to retrieve information (e.g. a partial search 'hint' result, or a new page to be displayed, etc...) you can use GET. If the data being sent is part of a request to change something (update a database, delete a record, etc..) then use POST.

Server-side, there's no reason to use the raw input, unless you want to grab the entire post/get data block in a single go. You can retrieve the specific information you want via the _GET/_POST arrays as usual. AJAX libraries such as MooTools/jQuery will handle the hard part of doing the actual AJAX calls and encoding form data into appropriate formats for you.

Marc B
That's the point: i want to grab the entire post/get data block in a single go, because JSON is a variable-less format, it rapresent just the data.
Manuel Bitto
@Kucebe I don't see why this is necessary, why not put the JSON data into a POST field and be done with it?
Pekka
If you want the entire JSON block, then why not assign the JSON text block to a form field and submit it like that? `<input type="hidden" name="data" value="json data here" />` is entirely acceptable and lets you retrieve it trivially server-side with $_REQUEST['data'].
Marc B
A: 

For JSON data, it's much easier to POST it as "application/json" content-type. If you use GET, you have to URL-encode the JSON in a parameter and it's kind of messy. Also, there is no size limit when you do POST. GET's size if very limited (4K at most).

ZZ Coder
A: 

"php://input allows you to read raw POST data. It is a less memory intensive alternative to $HTTP_RAW_POST_DATA and does not need any special php.ini directives. php://input is not available with enctype="multipart/form-data"." : http://php.net/manual/en/wrappers.php.php

zaf
A: 

On a related note, I'm sending a chunk of binary data to my php code and getting it via:

$putdata = file_get_contents('php://input');

I want to convert the contents to integers, but so far have had no luck. For example:

$putdata[1] seems to reference a string, not a number. All attempts so far to convert this value to a number return 48, which happens to be the ASCII code for the character '0' (zero).

I've tried casting, intval(), and some other things, and still don't get the actual number. Any help would be greatly appreciated.

mike dee
You need to use json_decode() function to decode JSON data:$putdata = json_decode(file_get_contents('php://input'));
Manuel Bitto