A friend of mine is writing an iOS application which will communicate with a web server. He needed a basic JSON program to test out his code, so I wrote him this:

<?php
if (isset($_REQUEST['json'])) {
  $decoded = json_decode(stripslashes($_REQUEST['json']), TRUE);
  if (is_null($decoded)) {
    $response['status'] = array(
      'type' => 'error',
      'value' => 'Invalid JSON value found',
    );
    $response['request'] = $_REQUEST['json'];
  }
  else {
    $response['status'] = array(
      'type' => 'message',
      'value' => 'Valid JSON value found',
    );
    //Send the original message back.
    $response['request'] = $decoded;
  }
}
else {
  $response['status'] = array(
    'type' => 'error',
    'value' => 'No JSON value set',
  );
}
$encoded = json_encode($response);
header('Content-type: application/json');
exit($encoded);

It accepts POST/GET JSON data passed as json, and gives a JSON response. If you save the code as json.php, you’ll be able to test it with: http://example.com/json.php?json={"a":1,"b":2,"c":3,"d":4,"e":5} Which returns:

{"status":{"type":"message","value":"Valid JSON value found"},"original":{"a":1,"b":2,"c":3,"d":4,"e":5}}

Comments

(Statically copied from previous site)

chandar replied on June 28, 2013 - 7:17am

Super, thanks and if u have some more please upload it..

John replied on August 14, 2014 - 3:18pm

Thank you for taking the time to post this Brad! Unfortunately it didn’t work for me. I got the error: Notice: Undefined index: json in /var/www/html/log/GRID.uploadCommand.php on line 25 {“status”:{“type”:“error”,“value”:“No JSON value set”},“request”:null}

line 25 being: $response[‘request’] = $_REQUEST[‘json’];

I guess my PHP doesn’t understand $_REQUEST[‘json’];

I did get it to work with: $_POST=array_merge($_POST,json_decode(file_get_contents(‘php://input’),true)); print_r($_POST);

But i’ve read doing it that way without sanitisation could be dangerous..

brad replied on August 15, 2014 - 2:49pm

Ah. Yes, I’ve deleted line 25. It’s only a notice, not an error; it was incorrect though.

Glad you got it to work!

AJ replied on May 25, 2015 - 10:05am

I copied/pasted the exact code to test it on my site, but I got the error message below:

Parse error: syntax error, unexpected ‘is’ (T_STRING), expecting :: (T_PAAMAYIM_NEKUDOTAYIM) in /home/newsonline/public_html/ws/getNews.php on line 2

The line 2 code should be: if (isset (link is external)($_REQUEST[‘json’])) {

Could someone help please..

Thank you so much !

Imtiaz replied on September 25, 2015 - 3:08am PERMALINK

if (isset (link is external)($_REQUEST[‘json’])) {

Remove the " (link is external)" text, it should read if (isset($_REQUEST[‘json’])) {

find amd replace all instances of " (link is external)" to “”, when copying and pasting, it adds that for some reason

javier replied on October 16, 2015 - 12:26am PERMALINK

AJ

You have to delete the senteces:

(link is external)

because it is not part of the code, these are just link to help you understand some parts of the code.

brad replied on October 26, 2015 - 10:49pm

Now that you mention it, I should look into removing that text. It’s probably getting in the way.

Update: It’s been removed.

Peter replied on January 19, 2017 - 11:59am

This post is still a life-saver after 6 years later! Thank you!