开发者

Passing JSON or similar object via URL and parsing in PHP

开发者 https://www.devze.com 2023-03-03 12:37 出处:网络
The situation is as follows: Due to circumstances beyond my control, I\'m writing a back-end which exposes 开发者_开发知识库data ONLY through JSON.

The situation is as follows:

Due to circumstances beyond my control, I'm writing a back-end which exposes 开发者_开发知识库data ONLY through JSON.

The UI is pure HTML/jQuery. All queries from the UI result in a JSON object (often quite complex). I need the UI to be able to pass me a complex object which includes multiple lists of Ids and strings.

What's the neatest way to implement this?

  • Multiple GET/POST params, some of which are lists eg &List=1,2,3
  • A Full JSON Object as a single Param eg ?Data={"List"=[1,2,3]}
  • Something else entirely.

If you've done something similar, I'd be interested in your experiences - especially with regards to passing object definitions to the client.

In this instance, imagine the following Data packet...

{
    Id: 1,
    Status: "Processing",
    RecordTypeAIds: (1,2,3)
    RecordTypeBIds: (4,5,6)
    RecordTypeCIds: null
}

What's the neatest way to send this information from jQuery to PHP (bearing in mind, lists may potentially be nested n-deep)

Thanks for any help you can provide.


Just to throw my opinion in there. I prefer JSON, just send the complex objects via your preferred method(GET/POST) and they almost instantly become an object/array in PHP using globals($_POST['key']) and json_decode(). If your using jQuery the 'sending' portion is easily achieved using $.ajax(), $.get(), or $.post(). POST theoretically has no datalimit so for arbitrarily large data sets it would be preferable.


Use the (nearly) all JSON solution of Data={... your object ...}. In PHP you need $data = json_decode($_REQUEST['Data']);. jQuery has a similar function for browser-side support.

For HTML forms and regular URLs .. write a server-side adapter that converts the arguments in $_REQUEST into the equivalent $data structure and calls the same code as above. If you name your HTML fields with PHP array notation then this can be as simple as $data = $_REQUEST;

Example in HTML

<input type='hidden' name='Status' value='Processing'>
<input type='hidden' name='RecordTypeAIds[0]' value='1'>
<input type='hidden' name='RecordTypeAIds[1]' value='2'>
<input type='hidden' name='RecordTypeAIds[2]' value='3'>

Appears in $_REQUEST as (a subset of) your example structure.

The trouble with PHP array notation in form elements is ... if you need to populate such a form from JSON then you must write an mildly complex decode function in Javascript to map a JSON structure into the correct field names. There is no good way to avoid the decode function because the DOM form name system is flat where JSON field names are nested, and for JSON array elements, the names (indices) are implicit. Using anything other than PHP array notation in your form element names only makes more work on the server-side.


If you want to pass a JSON object to PHP, then the required method is included by default in 5.2.0 and beyond. http://www.php.net/manual/en/function.json-decode.php

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号