<?php// Include the json classinclude('includes/json.php');
// Then create the PHP-Json Object to suits your needs
// Set a variable ; var name = {}$Json = new json('var', 'name');// Fire a callback ; callback({});$Json = new json('callback', 'name');// Just send a raw JSON ; {}$Json = new json();
// Build data$object = new stdClass();$object->test = 'OK';$arraytest = array('1','2','3');$jsonOnly = '{"Hello" : "darling"}';
// Add some content$Json->add('width', '565px');$Json->add('You are logged IN');$Json->add('An_Object', $object);$Json->add("An_Array",$arraytest);$Json->add("A_Json",$jsonOnly);
// Finally, send the JSON.
$Json->send();?>
<?phpheader("Content-Type: application/json");
// Collect what you need in the $data variable.
$json = json_encode($data);if ($json === false) {// Avoid echo of empty string (which is invalid JSON), and// JSONify the error message instead:$json = json_encode(["jsonError" => json_last_error_msg()]);if ($json === false) {// This should not happen, but we go all the way now:$json = '{"jsonError":"unknown"}';}// Set HTTP response status code to: 500 - Internal Server Errorhttp_response_code(500);}echo $json;?>
<?php
$db = mysqli_connect("localhost","root","","mylogs");//MSG$query = "SELECT * FROM logs LIMIT 20";$result = mysqli_query($db, $query);//Add all records to an array$rows = array();while($row = $result->fetch_array()){$rows[] = $row;}//Return result to jTable$qryResult = array();$qryResult['logs'] = $rows;echo json_encode($qryResult);
mysqli_close($db);
?>
<?php
// Set required headersheader('Content-Type: application/json; charset=utf-8');header('Access-Control-Allow-Origin: *');
/*** Example: First** Get JSON data from JSON file and retun as JSON response*/
// Get JSON data from JSON file$json = file_get_contents('response.json');
// Output, responseecho $json;
/** =. =.=. =.=. =.=. =.=. =.=. =.=. =.=. =.=. =.=. =. */
/*** Example: Second** Build JSON data from PHP array and retun as JSON response*/
// Or build JSON data from array (PHP)$json_var = ['hashtag' => 'HealthMatters','id' => '072b3d65-9168-49fd-a1c1-a4700fc017e0','sentiment' => ['negative' => 44,'positive' => 56,],'total' => '3400','users' => [['profile_image_url' => 'http://a2.twimg.com/profile_images/1285770264/PGP_normal.jpg','screen_name' => 'rayalrumbel','text' => 'Tweet (A), #HealthMatters because life is cool :) We love this life and want to spend more.','timestamp' => '\{\{$timestamp}}',],['profile_image_url' => 'http://a2.twimg.com/profile_images/1285770264/PGP_normal.jpg','screen_name' => 'mikedingdong','text' => 'Tweet (B), #HealthMatters because life is cool :) We love this life and want to spend more.','timestamp' => '\{\{$timestamp}}',],['profile_image_url' => 'http://a2.twimg.com/profile_images/1285770264/PGP_normal.jpg','screen_name' => 'ScottMili','text' => 'Tweet (C), #HealthMatters because life is cool :) We love this life and want to spend more.','timestamp' => '\{\{$timestamp}}',],['profile_image_url' => 'http://a2.twimg.com/profile_images/1285770264/PGP_normal.jpg','screen_name' => 'yogibawa','text' => 'Tweet (D), #HealthMatters because life is cool :) We love this life and want to spend more.','timestamp' => '\{\{$timestamp}}',],],];
// Output, responseecho json_encode($json_var);
JSON文件(JSON数据):
{"hashtag": "HealthMatters","id": "072b3d65-9168-49fd-a1c1-a4700fc017e0","sentiment": {"negative": 44,"positive": 56},"total": "3400","users": [{"profile_image_url": "http://a2.twimg.com/profile_images/1285770264/PGP_normal.jpg","screen_name": "rayalrumbel","text": "Tweet (A), #HealthMatters because life is cool :) We love this life and want to spend more.","timestamp": "\{\{$timestamp}}"},{"profile_image_url": "http://a2.twimg.com/profile_images/1285770264/PGP_normal.jpg","screen_name": "mikedingdong","text": "Tweet (B), #HealthMatters because life is cool :) We love this life and want to spend more.","timestamp": "\{\{$timestamp}}"},{"profile_image_url": "http://a2.twimg.com/profile_images/1285770264/PGP_normal.jpg","screen_name": "ScottMili","text": "Tweet (C), #HealthMatters because life is cool :) We love this life and want to spend more.","timestamp": "\{\{$timestamp}}"},{"profile_image_url": "http://a2.twimg.com/profile_images/1285770264/PGP_normal.jpg","screen_name": "yogibawa","text": "Tweet (D), #HealthMatters because life is cool :) We love this life and want to spend more.","timestamp": "\{\{$timestamp}}"}]}
/** returnJsonHttpResponse* @param $success: Boolean* @param $data: Object or Array*/function returnJsonHttpResponse($success, $data){// remove any string that could create an invalid JSON// such as PHP Notice, Warning, logs...ob_clean();
// this will clean up any previously added headers, to start cleanheader_remove();
// Set the content type to JSON and charset// (charset can be set to something else)header("Content-type: application/json; charset=utf-8");
// Set your HTTP response code, 2xx = SUCCESS,// anything else will be error, refer to HTTP documentationif ($success) {http_response_code(200);} else {http_response_code(500);}
// encode your PHP Object or Array into a JSON string.// stdClass or arrayecho json_encode($data);
// making sure nothing is addedexit();}
add_action( 'parse_request', function ($wp) {$data = /* Your data to serialise. */wp_send_json_success($data); /* Returns the data with a success flag. */exit(); /* Prevents more response from the server. */})