使用PHP怎么对本地进行API接口进行测试

发布时间:2021-03-09 16:51:37 作者:Leah
来源:亿速云 阅读:158

使用PHP怎么对本地进行API接口进行测试?相信很多没有经验的人对此束手无策,为此本文总结了问题出现的原因和解决方法,通过这篇文章希望你能解决这个问题。

1、前端页面文件post.html

使用PHP怎么对本地进行API接口进行测试

只是是简易的页面,没有复杂的布局,没有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>
<div 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 ><input class="refer" type="submit" value="提交"></p>
	</form>
</div>
</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();
?>

看完上述内容,你们掌握使用PHP怎么对本地进行API接口进行测试的方法了吗?如果还想学到更多技能或想了解更多相关内容,欢迎关注亿速云行业资讯频道,感谢各位的阅读!

推荐阅读:
  1. PHP中如何对Pest框架进行测试?
  2. 使用gomock怎么对mock进行测试

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

api php

上一篇:使用PHP怎么实现一个APP微信提现接口

下一篇:怎么在PHP中使用post方法传送json数据

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》