Php encapsulation json communication interface details and examples

Source: Internet
Author: User
This article mainly introduces the details of json communication interfaces encapsulated by php and related information of instances. For more information, see the following article. it mainly introduces the details of json communication interfaces encapsulated by php and related information of instances, for more information, see

JSON data creation in php:

 1, 'name' => 'David '); echo json_encode ($ arr); // is the key function for creating JSON?>

Implementation Result

{"id":1,"name":"david"}

Note: json_encode ($ value); this function can only receive UTF-8 encoded data. If data in other formats is transmitted to this function, null is returned;

Data method for encapsulating communication interfaces

1. standard communication data format:
0111 code status code (200,400). For example, if the logon succeeds at 200, the logon fails at 400.
Message (incorrect email format, 200 indicates successful logon)
Data returned data

Instance:

Demo. php

 $ Code, 'message' => $ message, 'data' => $ data); echo json_encode ($ result); exit ;}}?>



The main file test. PHP calls the method of the above class to create json data

 1, 'name' => 'David '); // call the json method of the Resonpse class Response: json (200, 'data returned successfully', $ arr);?>

Result of running test. php:

{"code":200,"message":"\u6570\u636e\u8fd4\u56de\u6210\u529f","data":{"id":1,"name":"david"}}

The above is the details of the json communication interface encapsulated by php and the content of the instance. For more information, please follow the PHP Chinese network (www.php1.cn )!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.