PHP - PHP 简易 Web 服务器、基础接口开发
一、PHP 简易 Web 服务器1、基本介绍PHP 自带一个简易的 Web 服务器适合快速测试启动方式如下php-S【监听地址】:【监听端口】# 例如php-S127.0.0.1:80002、注意事项通过以下方式启动就需要通过 localhost 访问而不能通过127.0.0.1访问php-Slocalhost:8000通过以下方式启动就可以通过127.0.0.1访问也可以通过 localhost 访问php-S127.0.0.1:8000二、基础接口开发1、GET 请求test/testGet/index.php?phpheader(Content-Type: text/plain);if($_SERVER[REQUEST_METHOD]!GET){http_response_code(405);echoError: Only GET method is allowed;exit;}echotestGet Hello World;exit;2、GET 请求携带参数test/testGetCarryData/index.php?phpheader(Content-Type: text/plain);if($_SERVER[REQUEST_METHOD]!GET){http_response_code(405);echoError: Only GET method is allowed;exit;}$str$_GET[str];echotestGetCarryData .$str;exit;3、RESTful GET 请求test/testGetRestful/index.php?phpheader(Content-Type: application/json);if($_SERVER[REQUEST_METHOD]!GET){http_response_code(405);echoError: Only GET method is allowed;exit;}$pathexplode(/,$_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[1newUser(1,jack,10),2newUser(2,tom,20),3newUser(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);echoError: Only POST method is allowed;exit;}$jsonfile_get_contents(php://input);$datajson_decode($json);classUser{public$id;public$name;public$age;publicfunction__construct($id,$name,$age){$this-id$id;$this-name$name;$this-age$age;}}$userMap[1newUser(1,jack,10),2newUser(2,tom,20),3newUser(3,smith,30)];echojson_encode($userMap[$data-id],JSON_PRETTY_PRINT);exit;
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2633165.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!