PHP本地进行API接口测试

最近写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。

  1. <html xmlns="http://blog.csdn.net/sinat_35861727?viewmode=contents">  
  2.   
  3. <head>  
  4.     <meta http-equiv = "Content-Type" content = "text/html;charset = utf8">  
  5.     <meta name = "description" content = "提交表单">  
  6.     <title>API接口请求表单</title>  
  7. </head>  
  8.     <style type="text/css">  
  9.         .key1{  
  10.             width:100px;  
  11.         }  
  12.         .value1{  
  13.             width:230px;  
  14.             margin:0 0 0 10px;  
  15.         }  
  16.         .main{  
  17.             margin:0 auto;  
  18.             width:450px;  
  19.             height:auto;  
  20.             background:lightgray;  
  21.             padding:40px 40px;  
  22.         }  
  23.         .refer{  
  24.             width:100px;  
  25.             height:24px;  
  26.         }  
  27.         .url{  
  28.             width:350px;  
  29.         }  
  30.     </style>  
  31. <body>  
  32.   
  33. <div class="main">  
  34.     <form method="POST" action="post.php" target="_blank">  
  35.         <p>请求地址:<input class="url" type="text" name="curl" placeholder="API接口地址"></p>  
  36.         <p>参 数1: <input class="key1" type="text" name="key1" placeholder="参数名">  
  37.                      <input class="value1" type="text" name="value1" placeholder="参数值"></p>  
  38.         <p>参 数2: <input class="key1" type="text" name="key2" placeholder="参数名">  
  39.                      <input class="value1" type="text" name="value2" placeholder="参数值"></p>  
  40.         <p>参 数3: <input class="key1" type="text" name="key3" placeholder="参数名">  
  41.                      <input class="value1" type="text" name="value3" placeholder="参数值"></p>  
  42.         <p>参 数4: <input class="key1" type="text" name="key4" placeholder="参数名">  
  43.                      <input class="value1" type="text" name="value4" placeholder="参数值"></p>  
  44.         <p>参 数5: <input class="key1" type="text" name="key5" placeholder="参数名">  
  45.                      <input class="value1" type="text" name="value5" placeholder="参数值"></p>  
  46.         <p>参 数6: <input class="key1" type="text" name="key6" placeholder="参数名">  
  47.                      <input class="value1" type="text" name="value6" placeholder="参数值"></p>  
  48.         <p>请求方式: <select name="method">  
  49.             <option value="POST">POST请求</option>  
  50.             <option value="GET">GET请求</option>  
  51.         </select></p>  
  52.         <p style="text-align:center;"><input class="refer" type="submit" value="提交"></p>  
  53.     </form>  
  54. </div>  
  55. </body>  
  56.   
  57.   
  58. </html>  

2.数据处理文件post.php

接收post.html页面传过来的数据,并发送请求然后处理请求结果,前端页面传过来的都是Body请求参数,如果还需要Header参数的话,可以在这个文件手动添加上去。

  1. <?php  
  2.   
  3. echo '<title>API接口请求响应</title>';  
  4.   
  5. /** 
  6.  * 设置网络请求配置 
  7.  * @param  [string] $curl 请求的URL 
  8.  * @param  [bool]   true || false 是否https请求 
  9.  * @param  [string] $method 请求方式,默认GET 
  10.  * @param  [array]  $header 请求的header参数 
  11.  * @param  [object] $data PUT请求的时候发送的数据对象 
  12.  * @return [object] 返回请求响应 
  13.  */  
  14. function ihttp_request($curl,$https=true,$method='GET',$header=array(),$data=null){  
  15.     // 创建一个新cURL资源  
  16.     $ch = curl_init();  
  17.       
  18.     // 设置URL和相应的选项  
  19.     curl_setopt($ch, CURLOPT_URL, $curl);    //要访问的网站  
  20.     //curl_setopt($ch, CURLOPT_HEADER, false);   
  21.     curl_setopt($ch, CURLOPT_HTTPHEADER, $header);   
  22.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);   
  23.   
  24.     if($https){  
  25.         curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);    
  26.         //curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, true);  
  27.     }  
  28.     if($method == 'POST'){  
  29.         curl_setopt($ch, CURLOPT_POST, true);  //发送 POST 请求  
  30.         curl_setopt($ch, CURLOPT_POSTFIELDS, $data);  
  31.     }  
  32.       
  33.       
  34.     // 抓取URL并把它传递给浏览器  
  35.     $content = curl_exec($ch);  
  36.     if ($content  === false) {  
  37.       return "网络请求出错: " . curl_error($ch);  
  38.       exit();  
  39.     }  
  40.     //关闭cURL资源,并且释放系统资源  
  41.     curl_close($ch);  
  42.       
  43.     return $content;  
  44. }  
  45.   
  46. //检查是否是链接格式  
  47. function checkUrl($C_url){    
  48.     $str="/^http(s?):\/\/(?:[A-za-z0-9-]+\.)+[A-za-z]{2,4}(?:[\/\?#][\/=\?%\-&~`@[\]\':+!\.#\w]*)?$/";    
  49.     if (!preg_match($str,$C_url)){    
  50.         return false;    
  51.     }else{    
  52.         return true;    
  53.     }    
  54. }    
  55.   
  56. //检查是不是HTTPS  
  57. function check_https($url){  
  58.     $str="/^https:/";  
  59.     if (!preg_match($str,$url)){    
  60.         return false;    
  61.     }else{    
  62.         return true;    
  63.     }    
  64. }  
  65.   
  66. if($_SERVER['REQUEST_METHOD'] != 'POST'exit('请求方式错误!');  
  67.   
  68.   
  69. //发送请求  
  70. function curl_query(){  
  71.     $data = array(  
  72.         $_POST['key1'] => $_POST['value1'],  
  73.         $_POST['key2'] => $_POST['value2'],  
  74.         $_POST['key3'] => $_POST['value3'],  
  75.         $_POST['key4'] => $_POST['value4'],  
  76.         $_POST['key5'] => $_POST['value5'],  
  77.         $_POST['key6'] => $_POST['value6']  
  78.     );  
  79.     //数组去空  
  80.     $data = array_filter($data);                    //post请求的参数  
  81.     if(empty($data)) exit('请填写参数');  
  82.       
  83.     $url = $_POST['curl'];                          //API接口  
  84.     if(!checkUrl($url)) exit('链接格式错误');     //检查连接的格式  
  85.     $is_https = check_https($url);                  //是否是HTTPS请求  
  86.   
  87.     $method = $_POST['method'];                     //请求方式(GET POST)  
  88.       
  89.     $header = array();                              //携带header参数  
  90.     //$header[] = 'Cache-Control: max-age=0';  
  91.     //$header[] = 'Connection: keep-alive';  
  92.       
  93.     if($method == 'POST'){  
  94.         $res = ihttp_request($url,$is_https,$method,$header,$data);  
  95.         print_r(json_decode($res,true));  
  96.     }else if($method == 'GET'){  
  97.         $curl = $url.'?'.http_build_query($data);   //GET请求参数拼接  
  98.         $res = ihttp_request($curl,$is_https,$method,$header);  
  99.         print_r(json_decode($res,true));  
  100.     }else{  
  101.         exit('error request method');  
  102.     }  
  103.   
  104. }  
  105.   
  106. curl_query();  
  107.   
  108. ?>  
写的很简单,功能也不是很全面,正常情况下的POST和GET请求还是可以满足的,至少本地测试查看结果是没有问题的,有需要的小伙伴可下载代码下来,然后根据自己的需求自行修改完善功能。下载地址: http://download.csdn.net/detail/sinat_35861727/9885450
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值