Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
A simple JSON response function for PHP. Used in various PhileCMS plugins.
<?php
function json_response($code = 200, $message = null)
{
// clear the old headers
header_remove();
// set the actual code
http_response_code($code);
// set the header to make sure cache is forced
header("Cache-Control: no-transform,public,max-age=300,s-maxage=900");
// treat this as json
header('Content-Type: application/json');
$status = array(
200 => '200 OK',
400 => '400 Bad Request',
422 => 'Unprocessable Entity',
500 => '500 Internal Server Error'
);
// ok, validation error, or failure
header('Status: '.$status[$code]);
// return the encoded json
return json_encode(array(
'status' => $code < 300, // success or not?
'message' => $message
));
}
// if you are doing ajax with application-json headers
if (empty($_POST)) {
$_POST = json_decode(file_get_contents("php://input"), true) ? : [];
}
// usage
echo json_response(200, 'working'); // {"status":true,"message":"working"}
// array usage
echo json_response(200, array(
'data' => array(1,2,3)
));
// {"status":true,"message":{"data":[1,2,3]}}
// usage with error
echo json_response(500, 'Server Error! Please Try Again!'); // {"status":false,"message":"Server Error! Please Try Again!"}
@ibariens

This comment has been minimized.

Copy link

@ibariens ibariens commented Jul 2, 2016

thank you for this, I had to change line 3 to: function json_response($code = 200, $message = null) for the usage example to work.

@mcuenez

This comment has been minimized.

Copy link

@mcuenez mcuenez commented Dec 15, 2016

thank you for this, I had to change line 3 to: function json_response($code = 200, $message = null) for the usage example to work.

You shouldn't do that, because that way you must use the $code parameter every time, even if you don't want to specify an error code other than 200.

You should rather change the call echo json_response(500, 'Server Error! Please Try Again!'); to echo json_response('Server Error! Please Try Again!', 500);

@hansspiess

This comment has been minimized.

Copy link

@hansspiess hansspiess commented Mar 4, 2017

@james2doyle seems like you're swapping arguments in your usage examples.

@amadeann

This comment has been minimized.

Copy link

@amadeann amadeann commented Jun 22, 2017

@james2doyle

Setting the error code does not work for me. Specifically, if I want to set the error code to 422 I get error 500. To make it work I had to change:

header('Status: '.$status[$code]);

to:

header('Status: '.$code);

Was there any reason why you were setting the status to the string value instead of the error code?

@saurabhmdh

This comment has been minimized.

Copy link

@saurabhmdh saurabhmdh commented Dec 21, 2017

Just swap the argument of method

json_response

.

Before : function json_response($message = null, $code = 200)

After : function json_response($code = 200, $message = null)

@Vanessa1906

This comment has been minimized.

Copy link

@Vanessa1906 Vanessa1906 commented Feb 11, 2019

dfdf

@ogabrielguerra

This comment has been minimized.

Copy link

@ogabrielguerra ogabrielguerra commented Apr 18, 2019

Thanks for sharing! Btw I think that @saurabhmdh is right. The parameters order were accidentally swapped.

@james2doyle

This comment has been minimized.

Copy link
Owner Author

@james2doyle james2doyle commented Jan 7, 2020

Updated the params. Can't believe they were swapped for so long... 🤕

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment