当前位置:网站首页>PHP three lines of code to write test interface
PHP three lines of code to write test interface
2022-07-21 20:53:00 【Amorous feelings】
Why does front-end development have to wait for interfaces every time . Isn't it fragrant to write one by yourself .
mock Local analog data idle trouble ,postman Writing interfaces is also troublesome .
Of course , Here I mean novice , Like to pursue simple and rough ways .
In fact, the essence of an interface is to return a string json object , So the simplest thing is :
One .json Text
1. Build a local environment , It can be phpstydy Integrate , It can also be wamp Integrate . Can provide web The service can be .
Such as lz utilize phpstudy Integrated local services
2. Create a test.json Text , It says what you want to return json data format .
as follows :json Don't be wrong about the format
3. And then visit locally :http://localhost:8080/test.json You can see the following effect :
The data looks like the correct back-end return data ,http://localhost:8080/test.json Is the request address , So can we go through ajax Where is the request ?
I'm sorry , I never tried. . I'm not sure .
A little off topic . Get down to business 、
Two .php Three lines of code simple interface
1. This step and json The text is the same , First set up local web Environmental Science
2. Create a test.php file
3. The following code must be written to allow cross domain .
<?php
header('Content-Type: text/html;charset=utf-8');
header('Access-Control-Allow-Origin:*'); // * Delegate allows any URL request
header('Access-Control-Allow-Methods:POST,GET,OPTIONS,DELETE'); // The type of request allowed
header('Access-Control-Allow-Credentials: true'); // Set whether to allow sending cookies
header('Access-Control-Allow-Headers:x-requested-with,content-type');
header("Content-Type: text/html; charset=utf-8");
header('Content-Type:application/json; charset=utf-8');?>
Here comes the key three lines of code
$json='{"all_rank_list": [{"area": " Huangyan District ","all_value": "7.3"},{"area": " Jiaojiang District ","all_value": "5.3"}]}';
$arr=json_decode($json,true);
echo json_encode($arr);
That's it , Access address http://localhost:8080/test.php You can return the corresponding json Data. . It can be used as the interface request address
边栏推荐
- Openfoam programming: combination of VOF method and porous media model
- PHP介绍
- PHP handles CSV files to solve Chinese garbled code
- php三行代码写测试接口
- express+ejs+swagger-ui-dist 打造及时更新的rest api 在线接口文档
- 17. [application of setw() function]
- BUUCTF [SUCTF 2019]EasySQL
- ECSHOP vulnerability recurrence
- JS小数转百分比出现的精度问题以及解决方法
- Porous media in openfoam
猜你喜欢
随机推荐
Help the great God
nodejs使用POST方法接收js对象并写入json文件
DVWA [SQL injection (blind)] learning record
Yar framework implements RPC
Boundary layer integral equation and Marangoni effect
Openfoam programming: combination of VOF method and porous media model
Storage principle of JS data type in memory
BUUCTF-web-随便注
解决uniapp编译后vendor.js文件过大(官方处理方案)
BUUCTF [GXYCTF2019] 禁止套娃
TP5对接免签FM支付接口
Nodejs+express uses CORS middleware to solve cross domain problems
Magic method of PHP
BUUCTF [SUCTF 2019]EasySQL
PHP date() function
JWT实现TP5用户登陆功能
web安全--文件包含(本地包含,远程包含)
[good article record] zorb framework construction process of embedded framework
ThinkPHP6 学习心得
打字机打字,退格效果