个人写的http接口测试页面,支持post和get参数提交

因为工作交接需要,花了一会写的一个类似在线接口测试页面,如果电脑能跑php,就可以直接使用,欢迎指点改进:

<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<title>接口測試</title>
</head>
<body>
<h1>接口測試</h1>
<form action="./jiekou.php" method="POST">
	接口地址:<input type="text" size="100" name="url" value="<?php if(!empty($_POST['url'])){echo $_POST['url'];}?>"><br/>
	參數列表:<input type="text" size="100" name="paramter_list" value="<?php if(!empty($_POST['paramter_list'])){echo $_POST['paramter_list'];}?>">參數1:值1 參數2:值2 ... 另:值是数组请用格式[XX,XX]<br/>
	提交方式:<input type="radio" name="type" value="post" <?php if(!empty($_POST['type']) && $_POST['type']=="post"){echo "checked='checked'";}?>>post <input type="radio" name="type" value="get" <?php if(!empty($_POST['type']) && $_POST['type']=="get"){echo "checked='checked'";}?>>get<br/>
	输出方式:<input type="radio" name="rtype" value="string" <?php if(!empty($_POST['rtype']) && $_POST['rtype']=="string"){echo "checked='checked'";}?>>json字符串 <input type="radio" name="rtype" value="array" <?php if(!empty($_POST['rtype']) && $_POST['rtype']=="array"){echo "checked='checked'";}?>>array<br/>
	<input type="submit"><input type="button" value="重新輸入" οnclick="location.href='./jiekou.php'"><input type="button" value="返回" οnclick="history.go(-1)">
</form>
</body>
</html>
<?php
if(!empty($_POST)){
	echo "<hr><h1>測試結果:</h1>";
	if(empty($_POST['url'])){
		echo "<font color='red'>接口地址不能為空</font>";
		die;
	}
	$url = $_POST['url'];
	$data = array();
	if(!empty($_POST['paramter_list'])){
		$paramter_list = str_replace(':', ':', $_POST['paramter_list']);
		$paramter_list = trim($paramter_list);
		$paramter_arr = explode(' ', $paramter_list);
		foreach ($paramter_arr as $key => $value) {
			$k = explode(':', $value)[0];
			$v = explode(':', $value)[1];
			if(!empty($_POST['type']) && $_POST['type'] == "post" && strpos($v,'[') === 0 && strpos($v,']') == mb_strlen($v)-1){
				$v = trim(trim($v,'['),']');
				$v = str_replace(',', ',', $v);
				$v = explode(',',$v);
			}
			$data[$k] = $v;
		}
		if(!empty($_POST['type']) && $_POST['type'] == "get"){
			$paramter_list = str_replace(':', '=', $paramter_list);
			$paramter_list = str_replace(' ', '&', $paramter_list);
			$url .= strpos($url,'?') ? "&" . $paramter_list : "?" . $paramter_list;
		}
	}
	// curl獲取接口
	$ch =  curl_init();
	curl_setopt($ch, CURLOPT_URL, $url);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
	if(!empty($_POST['type']) && $_POST['type'] == "get"){
		curl_setopt($ch, CURLOPT_HEADER, 0);
	}else{
		// post数据
		curl_setopt($ch, CURLOPT_POST, 1);
		// post的变量
		curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
	}

	$output = curl_exec($ch);
	curl_close($ch);

	if(!empty($_POST['rtype']) && $_POST['rtype'] == "string"){
		echo $output;die;
	}
	$output = json_decode($output,true);

	//打印获得的数据
	echo "<pre>";
	print_r($output);
}
?>

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值