php encapsulates json communication interface
Detailed explanation of creating JSON data:
$arr=array(
'id'=>1,
'name'=>'david'
);
echo json_encode($arr);//This is the key function to create JSON
?>
Achieve results
{"id":1,"name":"david"}
Note: json_encode($value); this function can only receive UTF-8 encoded data. If data in other formats is passed to this function, null will be returned;
Encapsulate the data method of the communication interface
1. Communication data format standard:
0111 code status code (200,400) such as: 200 for successful login, 400 for unsuccessful login
message prompt message (email format is incorrect, 200 means login successful)
data return data
Example:
demo.php
class Response{
/**
*Output communication data in json format
*@param integer $code status code
*@param string $message prompt message
*@param array $data data
*return string The return value is json
*/
public static function json($code,$message='',$data=array()){
if(!is_numeric($code)){
return '';
}
$result=array(
'code'=>$code,
'message'=>$message,
'data'=>$data
);
echo json_encode($result);
exit;
}
test.php
http://www.bkjia.com/PHPjc/927610.htmlwww.bkjia.comtruehttp: //www.bkjia.com/PHPjc/927610.htmlTechArticlephp encapsulates json communication interface to create JSON data detailed explanation: $arr=array( 'id'=>1, 'name '=>'david' ); echo json_encode($arr);//Is this the key function to create JSON?> Implementation result {"id":1,"nam...