今天因為工作的需要,上網研究了一下這個功能,便進行了測試,因為我要在一個網站上面使用它免費提供的API數據,於是乎就找到了一些相關教學文章。
先看下面這段HTML源碼:
<html xmlns="http://blog.csdn.net/sinat_35861727?viewmode=contents">
<head>
<meta http-equiv ="Content-Type" content ="text/html;charset =utf8"/>
<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<meta name="renderer" content="webkit|ie-comp|ie-stand">
<meta name="robots" content="nofollow,noindex"/>
<meta name ="description" content ="提交表单"/>
<title>API接口请求表单</title>
</head>
<style type="text/css">
.key{
width:100px;
}
.value{
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">
<h1>api网址请求测试用途</h1>
<p>请求地址:<input class="url" type="text" name="curl" placeholder="API接口地址"></p>
<ul>
<li>参数1: <input class="key" type="text" name="key1" placeholder="参数名"><input class="value" type="text" name="value1" placeholder="参数值"></li>
<li>参数2: <input class="key" type="text" name="key2" placeholder="参数名"><input class="value" type="text" name="value2" placeholder="参数值"></li>
<li>参数3: <input class="key" type="text" name="key3" placeholder="参数名"><input class="value" type="text" name="value3" placeholder="参数值"></li>
<li>参数4: <input class="key" type="text" name="key4" placeholder="参数名"><input class="value" type="text" name="value4" placeholder="参数值"></li>
<li>参数5: <input class="key" type="text" name="key5" placeholder="参数名"><input class="value" type="text" name="value5" placeholder="参数值"></li>
<li>参数6: <input class="key" type="text" name="key6" placeholder="参数名"><input class="value" type="text" name="value6" placeholder="参数值"></li>
<li>参数7: <input class="key" type="text" name="key7" placeholder="参数名"><input class="value" type="text" name="value7" placeholder="参数值"></li>
<li>参数8: <input class="key" type="text" name="key8" placeholder="参数名"><input class="value" type="text" name="value8" placeholder="参数值"></li>
<li>参数9: <input class="key" type="text" name="key9" placeholder="参数名"><input class="value" type="text" name="value9" placeholder="参数值"></li>
<li>参数10: <input class="key" type="text" name="key10" placeholder="参数名"><input class="value" type="text" name="value10" placeholder="参数值"></li>
</ul>
<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>
</div>
</body>
</html>
上述源碼所呈現的樣子如下圖:
ok,前台源碼搞定好之後,接下來要寫php去控制它。
<?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'],
$_POST['key7'] => $_POST['value7'],
$_POST['key8'] => $_POST['value8'],
$_POST['key9'] => $_POST['value9'],
$_POST['key10'] => $_POST['value10'],
);
//数组去空
$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();
?>
這兩個檔案上傳到server底下,訪問post.html,把你想要調用的api數據網址貼上,根據api的參數對照上去,接著按提交即可。