0

I currently have an server API to receive POST with JSON data. The JSON data is as below

The JSON object is as below

[{
          firstName : "XXXXX",
          surName   : "XXXXXX",
          contact   : "XXXXXXX",
          branch    : "XXXXXX",
          reportsTo : "XXXXX",
          startTime : "9:00AM",
          endTime   : "6:00PM"},
  {
          firstName : "XXXXXX",
          surName   : "XXXXXXX",
          contact   : "XXXXXXX",
          branch    : "XXXXXXX",
          reportsTo : "XXXXXXX",
          startTime : "9:00AM",
          endTime   : "6:00PM"
}];

Now i need to change code to support GET for same JSON data. The JSON object contains multiple json objects. With two elements in Array is something like below.

APP_ID=XXXXXXXf&client=XXXXXXXXXX&Details=%7B%22firstName%22:%22XXXXXXX%22,%22surName%22:%22lXXXXXXX%22,%22contact%22:%22XXXXXXX%22,%22branch%22:%22XXXXXXX%22,%22reportsTo%22:%22XXXXXXX%22,%22startTime%22:%229:00AM%22,%22endTime%22:%226:00PM%22%7D&professionalDetails=%7B%22firstName%22:%22XXXXXXX%22,%22surName%22:%22XXXXXXX%22,%22contact%22:%22XXXXXXX%22,%22branch%22:%22XXXXXXX%22,%22reportsTo%22:%22XXXXXXX%22,%22startTime%22:%229:00AM%22,%22endTime%22:%226:00PM%22%7D&timeStamp=1378950760

  1. Is GET an method that can be used for such cases?

  2. Any efficient method to extract the data without much string manipulation?

de-bugged
  • 935
  • 4
  • 14
  • 34
  • there is a limit to how long the query string can be. Which i believe is followed by the most popular web servers. so using get method you will be more limited to the amount of data you can passthrough – DevZer0 Sep 12 '13 at 02:16

4 Answers4

0

You'll find GET will fail if the JSON array gets too large, and too large is not very big. POST is the way to go here.

Once you have the data in your PHP script you can decode it with

$myData = json_decode($_POST['myData']);

After which you can access it as a PHP variable:

$firstFirstName = $myData[0]->firstName;
  • thnx for the update. I already have this POST code set up. My colleague, needs the code in GET so he can integrate his iOS Code to it. – de-bugged Sep 12 '13 at 02:29
  • Are you saying your colleague is requesting the data, rather than sending it? If so, this is not clear in your question and my answer is not pertinent. –  Sep 12 '13 at 02:59
  • colleague is confused on sending POST data from his app. So he ask for setting up GET data, so he can use GET data, which he is familiar with programming – de-bugged Sep 12 '13 at 03:24
  • He shouldn't be using GET for sending data. 1) it's contrary to convention laid out in the HTTP 1.1 spec; 2) it's far too easy to hit the button over and over and send the same data repeatedly; 3) exactly what servers will accept as a URL&querystring varies from server to server - expect 413 or 414 errors to occur sooner or later. –  Sep 12 '13 at 03:34
0

You can use GET, but you are limited at about 2000 characters (See here)

Use json_decode(). You find the doc on php.net.

Community
  • 1
  • 1
Lorenz Meyer
  • 19,166
  • 22
  • 75
  • 121
0

Im not a pro. But what i would do is.

change all $_POST to $_REQUEST

then make this

if(isset($_GET['firstName']))
{
 foreach($_RESQUEST as $k => $v)
 {
  $_RESQUEST[$k] = url_decode($v);
 }
}
Nicolas Racine
  • 1,031
  • 3
  • 13
  • 35
0

You can use get method in your same code instead of post.. the json parsing will go the same way.. instead of $_POST['josn_str'] you need to use $_GET['json_str']

then I think you are already be using json_decode

only thing will be using GET you will get less data in API from a single call.

dkkumargoyal
  • 556
  • 3
  • 10