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. This article mainly introduces the PHP API interface necessary output JSON format data related to the text through the sample code introduced in very detailed, for everyone has a certain reference learning value, hope to help everyone.
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.