Home >Backend Development >PHP Tutorial >返回JSON数据的接口

返回JSON数据的接口

WBOY
WBOYOriginal
2016-06-23 14:01:161053browse

需要写一个只给出连接就可以返回JSON数据的接口 如何实现 请给出思路


回复讨论(解决方案)

给出连接 输出 json?是这个意思?

$arr = array('连接成功');echo json_encode($arr);

最简单的 不过这样肯定不行的 哈哈

给出连接 输出 json?是这个意思?

差不多吧 
就是给一个连接 可以通过连接拼的参数不同来返回不同的数据 

不是用switch 就是用if.

http://sxxxxx/json.php?type=1
http://sxxxxx/json.php?type=2

header('Content-type: application/json');$type = isset($_GET['type']) ? intval($_GET['type']) : 1;$array = array();if ($type == 1) {  $array = array("test" => "I'm 1");} else if ($type == 2) {  $array = array("test" => "I'm 2");}echo json_encode($array);

http://sxxxxx/json.php?type=1
http://sxxxxx/json.php?type=2

header('Content-type: application/json');$type = isset($_GET['type']) ? intval($_GET['type']) : 1;$array = array();if ($type == 1) {  $array = array("test" => "I'm 1");} else if ($type == 2) {  $array = array("test" => "I'm 2");}echo json_encode($array);

++

Statement:
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