This article mainly introduces you to the PHP API interface necessary output JSON format data related to the text in the sample code introduced in very detailed, for everyone has a certain reference learning value, the need for friends below to see together.
Objective
We have to write the interface in the daily development work, the output of JSON-formatted text is the production interface must be mastered, now the interface is basically in JSON format data. The following words do not say, to see the details of this article:
Simple php json file output:
<?php$data =array (' request ' = ' success ', ' msg ' = ' 35235 '); $data _json = Json_encode ($data); header (' Content-type:text/json '); echo $data _json;? >
In this way, we can get a page in the form of JSON format, which can be modified using the reference.
How to read an interface JSON file:
<?php$data =file_get_contents (' Http://api.1yhaha.cn/v1/sms/send-validate-code '); $data _new = Json_decode ($data) ; Var_dump ($data _new);? >
Obviously, the interface address is: Http://api.1yhaha.cn/v1/sms/send-validate-code, I was through http://api.1yhaha.cn/v1/sms/ Send-validate-code this address to read the information, read out the data is in JSON format, need to decode, the resulting $data_new will be a data object, how to use the data you will know the print.
Related recommendations:
How to use Ajax to receive JSON data in thinkphp, thinkphpjson_php tutorial
PHP JSON operation, Phpjson operation _php Tutorial
Thinkphp two implementations of JSON return via Ajax, thinkphpjson_php tutorial