PHP encapsulates json communication interface_PHP tutorial

WBOY
Release: 2016-07-13 10:11:47
Original
932 people have browsed it

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

www.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...
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!