<?phpheader('Content-Type: text/plain');if($_SERVER['REQUEST_METHOD']!=='GET'){http_response_code(405);echo'Error: Only GET method is allowed';exit;}echo"testGet Hello World";exit;
2、GET 请求携带参数
test/testGetCarryData/index.php
<?phpheader('Content-Type: text/plain');if($_SERVER['REQUEST_METHOD']!=='GET'){http_response_code(405);echo'Error: Only GET method is allowed';exit;}$str=$_GET['str'];echo"testGetCarryData ".$str;exit;
3、RESTful GET 请求
test/testGetRestful/index.php
<?phpheader('Content-Type: application/json');if($_SERVER['REQUEST_METHOD']!=='GET'){http_response_code(405);echo'Error: Only GET method is allowed';exit;}$path=explode('/',$_SERVER['REQUEST_URI']);$id=(int)end($path);classUser{public$id;public$name;public$age;publicfunction__construct($id,$name,$age){$this->id=$id;$this->name=$name;$this->age=$age;}}$userMap=[1=>newUser(1,"jack",10),2=>newUser(2,"tom",20),3=>newUser(3,"smith",30)];echojson_encode($userMap[$id],JSON_PRETTY_PRINT);exit;
4、POST 请求
test/testPost/index.php
<?phpheader('Content-Type: application/json');if($_SERVER['REQUEST_METHOD']!=='POST'){http_response_code(405);echo'Error: Only POST method is allowed';exit;}$json=file_get_contents('php://input');$data=json_decode($json);classUser{public$id;public$name;public$age;publicfunction__construct($id,$name,$age){$this->id=$id;$this->name=$name;$this->age=$age;}}$userMap=[1=>newUser(1,"jack",10),2=>newUser(2,"tom",20),3=>newUser(3,"smith",30)];echojson_encode($userMap[$data->id],JSON_PRETTY_PRINT);exit;