最近写api接口,每写一个接口,我自己需要先测试一下,看有没有语法错误,请求的数据对不对,但是很多都是post请求,没法直接在浏览器中打开链接进行测试,所以必须要有个可以在本地发http请求的模拟工具,模拟一下数据请求。
一开始我是这么干的,在本机wampserver运行目录下创建一个文件,在里边写curl请求,进行模拟请求测试,但是每个接口需要的参数都不一样,我需要不断地修改请求的参数和api,很是不方便。到后来我的这个请求文件里边乱糟糟的数据,我都分不清了:
在网上找了找相关的工具,有不少在线测试的,比如:atool在线工具、apizza等等,看了下他们做的都很好,使用非常方便,界面很漂亮,服务也很周到。但是我在考虑安全问题,同时它给我返回的是原始的json格式的数据,我习惯于看数组格式的,比较直观。
于是乎,本着自己动手丰衣足食的理念,我就在本地写一个简易的api测试页面,提交数据之后在本地实现api请求测试功能,既不用考虑安全问题,又可以对结果随便转换。只需要两个文件就搞定,一个是填写数据的页面post.html,另一个是接收post.html页面传过来的数据并处理请求实现功能的post.php文件。
1.前端页面文件post.html
只是是简易的页面,没有复杂的布局,没有js特效,暂时只写了6个参数,一般来说也够了,不够的可以自行添加。这里默认传的都是body请求参数,请求方式也只使用了get和post。
<html xmlns="http://blog.csdn.net/sinat_35861727?viewmode=contents">
<head>
<meta http-equiv = "content-type" content = "text/html;charset = utf8">
<meta name = "description" content = "提交表单">
<title>api接口请求表单</title>
</head>
<style type="text/css">
.key1{
width:100px;
}
.value1{
width:230px;
margin:0 0 0 10px;
}
.main{
margin:0 auto;
width:450px;
height:auto;
background:lightgray;
padding:40px 40px;
}
.refer{
width:100px;
height:24px;
}
.url{
width:350px;
}
</style>
<body>
<p class="main">
<form method="post" action="post.php" target="_blank">
<p>请求地址:<input class="url" type="text" name="curl" placeholder="api接口地址"></p>
<p>参 数1: <input class="key1" type="text" name="key1" placeholder="参数名">
<input class="value1" type="text" name="value1" placeholder="参数值"></p>
<p>参 数2: <input class="key1" type="text" name="key2" placeholder="参数名">
<input class="value1" type="text" name="value2" placeholder="参数值"></p>
<p>参 数3: <input class="key1" type="text" name="key3" placeholder="参数名">
<input class="value1" type="text" name="value3" placeholder="参数值"></p>
<p>参 数4: <input class="key1" type="text" name="key4" placeholder="参数名">
<input class="value1" type="text" name="value4" placeholder="参数值"></p>
<p>参 数5: <input class="key1" type="text" name="key5" placeholder="参数名">
<input class="value1" type="text" name="value5" placeholder="参数值"></p>
<p>参 数6: <input class="key1" type="text" name="key6" placeholder="参数名">
<input class="value1" type="text" name="value6" placeholder="参数值"></p>
<p>请求方式: <select name="method">
<option value="post">post请求</option>
<option value="get">get请求</option>
</select></p>
<p style="text-align:center;"><input class="refer" type="submit" value="提交"></p>
</form>
</p>
</body>
</html>
2.数据处理文件post.php
接收post.html页面传过来的数据,并发送请求然后处理请求结果,前端页面传过来的都是body请求参数,如果还需要header参数的话,可以在这个文件手动添加上去。
<?php
echo '<title>api接口请求响应</title>';
/**
* 设置网络请求配置
* @param [string] $curl 请求的url
* @param [bool] true || false 是否https请求
* @param [string] $method 请求方式,默认get
* @param [array] $header 请求的header参数
* @param [object] $data put请求的时候发送的数据对象
* @return [object] 返回请求响应
*/
function ihttp_request($curl,$https=true,$method='get',$header=array(),$data=null){
// 创建一个新curl资源
$ch = curl_init();
// 设置url和相应的选项
curl_setopt($ch, curlopt_url, $curl); //要访问的网站
//curl_setopt($ch, curlopt_header, false);
curl_setopt($ch, curlopt_httpheader, $header);
curl_setopt($ch, curlopt_returntransfer, true);
if($https){
curl_setopt($ch, curlopt_ssl_verifypeer, false);
//curl_setopt($ch, curlopt_ssl_verifyhost, true);
}
if($method == 'post'){
curl_setopt($ch, curlopt_post, true); //发送 post 请求
curl_setopt($ch, curlopt_postfields, $data);
}
// 抓取url并把它传递给浏览器
$content = curl_exec($ch);
if ($content === false) {
return "网络请求出错: " . curl_error($ch);
exit();
}
//关闭curl资源,并且释放系统资源
curl_close($ch);
return $content;
}
//检查是否是链接格式
function checkurl($c_url){
$str="/^http(s?):\/\/(?:[a-za-z0-9-]+\.)+[a-za-z]{2,4}(?:[\/\?#][\/=\?%\-&~`@[\]\':+!\.#\w]*)?$/";
if (!preg_match($str,$c_url)){
return false;
}else{
return true;
}
}
//检查是不是https
function check_https($url){
$str="/^https:/";
if (!preg_match($str,$url)){
return false;
}else{
return true;
}
}
if($_server['request_method'] != 'post') exit('请求方式错误!');
//发送请求
function curl_query(){
$data = array(
$_post['key1'] => $_post['value1'],
$_post['key2'] => $_post['value2'],
$_post['key3'] => $_post['value3'],
$_post['key4'] => $_post['value4'],
$_post['key5'] => $_post['value5'],
$_post['key6'] => $_post['value6']
);
//数组去空
$data = array_filter($data); //post请求的参数
if(empty($data)) exit('请填写参数');
$url = $_post['curl']; //api接口
if(!checkurl($url)) exit('链接格式错误'); //检查连接的格式
$is_https = check_https($url); //是否是https请求
$method = $_post['method']; //请求方式(get post)
$header = array(); //携带header参数
//$header[] = 'cache-control: max-age=0';
//$header[] = 'connection: keep-alive';
if($method == 'post'){
$res = ihttp_request($url,$is_https,$method,$header,$data);
print_r(json_decode($res,true));
}else if($method == 'get'){
$curl = $url.'?'.http_build_query($data); //get请求参数拼接
$res = ihttp_request($curl,$is_https,$method,$header);
print_r(json_decode($res,true));
}else{
exit('error request method');
}
}
curl_query();
?>
写的很简单,功能也不是很全面,正常情况下的post和get请求还是可以满足的,至少本地测试查看结果是没有问题的,有需要的小伙伴可下载代码下来,然后根据自己的需求自行修改完善功能。
相关推荐:
php api接口测试小工具
以上就是php进行api接口测试的详细内容。