热搜:NVER node 开发 php

返回JSON数据的接口

2024-09-03 18:10:01
返回JSON数据的接口

需要写一个只给出连接就可以返回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);

++