• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

如何在本地进行API接口测试

php 搞代码 3年前 (2022-01-22) 14次浏览 已收录 0个评论

这次给大家带来如何在本地进行API接口测试,在本地进行API接口测试的注意事项有哪些,下面就是实战案例,一起来看一下。

只是是简易的页面,没有复杂的布局,没有JS特效,暂时只写了6个参数,一般来说也够了,不够的可以自行添加。这里默认传的都是body请求参数,请求方式也只使用了GET和POST。

<html xmlns="http://blog.gaodaima.com/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参数的话,可以在这个文件手动添加上去。

<?phpecho '<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, CURLO<em>/本2文来源[email protected]搞@^&代*@码2网</em><strong>搞gaodaima代码</strong>PT_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;  } } //检查是不是HTTPSfunction 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请求还是可以满足的,至少本地测试查看结果是没有问题的,有需要的小伙伴可下载代码下来,然后根据自己的需求自行修改完善功能。

相信看了本文案例你已经掌握了方法,更多精彩请关注搞代码其它相关文章!

推荐阅读:

phpstudy2018的访问目录服务权限

ThinkPHP实现微信支付(jsapi支付)流程教程详解_php实例

以上就是如何在本地进行API接口测试的详细内容,更多请关注搞代码gaodaima其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:如何在本地进行API接口测试
喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址