|
如果有个JSON接口需授权IP或域名使用,那就给它套个,然后就可随意调用,也可以自定义返回自己想要的内容,废话不多说,代码如下:
<?php
//资源宝分享:www.httple.net
header(&#34;content-type:application/json;charset=utf8&#34;);
$url = $_GET[&#39;url&#39;];
if($_GET[&#39;url&#39;]==&#39;&#39;){
$arr = [
&#39;code&#39; => 404,
&#39;msg&#39; => &#39;缺少URL&#39;,
&#39;from&#39; => &#39;api.leafone.cn&#39;
];
die(json_encode($arr, JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES));
}
$Api=&#39;&#39;; //JSON接口
$jiexi = $Api.$url;
$html = httpget($jiexi);
$json = json_decode($html, true);
$vurl=$json[&#39;url&#39;];
if($vurl ==&#39;&#39;||$vurl ==null){
$arr = [
&#39;code&#39; => 100,
&#39;msg&#39; => &#39;解析失败&#39;,
&#39;link&#39; => $url,
&#39;from&#39; => &#39;api.leafone.cn&#39;
];
die(json_encode($arr, JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES));
}
$arr = [
&#39;code&#39; => 200,
&#39;msg&#39; => &#39;解析成功&#39;,
&#39;url&#39; => $vurl,
&#39;link&#39; => $url,
&#39;from&#39; => &#39;api.leafone.cn&#39;
];
die(json_encode($arr, JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES));
function httpget($url, $timeout = 10)
{
$ch = curl_init(); //初始化 curl
curl_setopt($ch, CURLOPT_URL, $url); //要访问网页 URL 地址
curl_setopt($ch, CURLOPT_NOBODY, false); //设定是否输出页面内容
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //返回字符串,而非直接输出到屏幕上
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, false); //连接超时时间,设置为 0,则无限等待
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); //数据传输的最大允许时间超时,设为一小时
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY); //HTTP验证方法
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //不检查 SSL 证书来源
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //不检查 证书中 SSL 加密算法是否存在
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); //跟踪爬取重定向页面
curl_setopt($ch, CURLOPT_AUTOREFERER, true); //当Location:重定向时,自动设置header中的Referer:信息
curl_setopt($ch, CURLOPT_ENCODING, &#39;&#39;); //解决网页乱码问题
curl_setopt($ch, CURLOPT_REFERER, $_SERVER[&#39;HTTP_REFERER&#39;]);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER[&#39;HTTP_USER_AGENT&#39;]);
$httpheaders = array();
$httpheaders[] = &#34;CLIENT-IP: {$_SERVER[&#39;HTTP_CLIENT_IP&#39;]}&#34;;
$httpheaders[] = &#34;X-FORWARDED-FOR: {$_SERVER[&#39;HTTP_X_FORWARDED_FOR&#39;]}&#34;;
curl_setopt($ch, CURLOPT_HTTPHEADER, $httpheaders);
$data = curl_exec($ch); //运行 curl,请求网页并返回结果
curl_close($ch); //关闭 curl
return $data;
}
?>多个方式:
<?php
//资源宝分享:www.httple.net
//JSON格式化
header(&#34;content-type:application/json;charset=utf8&#34;);
//当URL为空时返回内容
$url = $_GET[&#39;url&#39;];
if($_GET[&#39;url&#39;] == &#39;&#39;) {
$arr = [
&#39;code&#39; => 404,
&#39;msg&#39; => &#39;缺少URL!&#39;,
&#39;from&#39; => $_SERVER[&#39;HTTP_HOST&#39;]
];
die(json_encode($arr, JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES));
}
$json1 = &#39;&#39;; //接口1
$json2 = &#39;&#39;; //接口2
$json3 = &#39;&#39;; //接口3
$json4 = &#39;&#39;; //接口4
$json5 = &#39;&#39;; //接口5
//接口轮询
$jiexi = $json1.$url;
$html = httpget($jiexi);
$data = json_decode($html, true);
if ($data[&#39;url&#39;] == &#39;&#39; || $data[&#39;url&#39;] == null) {
$jiexi = $json2.$url;
$html = httpget($jiexi);
$data = json_decode($html, true);
}
if ($data[&#39;url&#39;] == &#39;&#39; || $data[&#39;url&#39;] == null) {
$jiexi = $json3.$url;
$html = httpget($jiexi);
$data = json_decode($html, true);
}
if ($data[&#39;url&#39;] == &#39;&#39; || $data[&#39;url&#39;] == null) {
$jiexi = $json4.$url;
$html = httpget($jiexi);
$data = json_decode($html, true);
}
if ($data[&#39;url&#39;] == &#39;&#39; || $data[&#39;url&#39;] == null) {
$jiexi = $json5.$url;
$html = httpget($jiexi);
$data = json_decode($html, true);
}
if ($data[&#39;url&#39;] == &#39;&#39; || $data[&#39;url&#39;] == null) {
$arr = [
&#39;code&#39; => 100,
&#39;msg&#39; => &#39;解析失败&#39;,
&#39;link&#39; => $url,
&#39;from&#39; => $_SERVER[&#39;HTTP_HOST&#39;]
];
die(json_encode($arr, JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES));
}
$arr = [
&#39;code&#39; => 200,
&#39;msg&#39; => &#39;解析成功&#39;,
&#39;url&#39; => $data[&#39;url&#39;],
&#39;link&#39; => $url,
&#39;from&#39; => $_SERVER[&#39;HTTP_HOST&#39;]
];
die(json_encode($arr, JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES));
function httpget($url, $timeout = 5)
{
$ch = curl_init(); //初始化 curl
curl_setopt($ch, CURLOPT_URL, $url); //要访问网页 URL 地址
curl_setopt($ch, CURLOPT_NOBODY, false); //设定是否输出页面内容
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //返回字符串,而非直接输出到屏幕上
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, false); //连接超时时间,设置为 0,则无限等待
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); //数据传输的最大允许时间超时,设为一小时
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY); //HTTP验证方法
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //不检查 SSL 证书来源
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //不检查 证书中 SSL 加密算法是否存在
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); //跟踪爬取重定向页面
curl_setopt($ch, CURLOPT_AUTOREFERER, true); //当Location:重定向时,自动设置header中的Referer:信息
curl_setopt($ch, CURLOPT_ENCODING, &#39;&#39;); //解决网页乱码问题
curl_setopt($ch, CURLOPT_REFERER, $_SERVER[&#39;HTTP_REFERER&#39;]);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER[&#39;HTTP_USER_AGENT&#39;]);
$httpheaders = array();
$httpheaders[] = &#34;CLIENT-IP: {$_SERVER[&#39;HTTP_CLIENT_IP&#39;]}&#34;;
$httpheaders[] = &#34;X-FORWARDED-FOR: {$_SERVER[&#39;HTTP_X_FORWARDED_FOR&#39;]}&#34;;
curl_setopt($ch, CURLOPT_HTTPHEADER, $httpheaders);
$data = curl_exec($ch); //运行 curl,请求网页并返回结果
curl_close($ch); //关闭 curl
return $data;
}
?>while循环:
<?php
//资源宝分享:www.httple.net
//JSON格式化
header(&#34;content-type:application/json;charset=utf8&#34;);
//当URL为空时返回内容
$url = $_GET[&#39;url&#39;];
if($_GET[&#39;url&#39;] == &#39;&#39;) {
$arr = [
&#39;code&#39; => 404,
&#39;msg&#39; => &#39;缺少URL!&#39;,
&#39;from&#39; => $_SERVER[&#39;HTTP_HOST&#39;]
];
die(json_encode($arr, JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES));
}
$json = array(
&#39;&#39;,
&#39;&#39;,
&#39;&#39;,
&#39;&#39;,
&#39;&#39;,
); //接口
//接口轮询
$num = 0;
$jiexi = $json[$num].$url;
$html = httpget($jiexi);
$data = json_decode($html, true);
while ($data[&#39;url&#39;] == &#39;&#39; || $data[&#39;url&#39;] == null) {
$jiexi = $json[$num+1].$url;
$html = httpget($jiexi);
$data = json_decode($html, true);
$num = $num + 1;
if ($num >= count($json)) {
break;
}
}
if ($data[&#39;url&#39;] == &#39;&#39; || $data[&#39;url&#39;] == null) {
$arr = [
&#39;code&#39; => 100,
&#39;msg&#39; => &#39;解析失败&#39;,
&#39;link&#39; => $url,
&#39;from&#39; => $_SERVER[&#39;HTTP_HOST&#39;]
];
die(json_encode($arr, JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES));
}
else {
$arr = [
&#39;code&#39; => 200,
&#39;msg&#39; => &#39;解析成功&#39;,
&#39;url&#39; => $data[&#39;url&#39;],
&#39;link&#39; => $url,
&#39;from&#39; => $_SERVER[&#39;HTTP_HOST&#39;]
];
die(json_encode($arr, JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES));
}
function httpget($url, $timeout = 3)
{
$ch = curl_init(); //初始化 curl
curl_setopt($ch, CURLOPT_URL, $url); //要访问网页 URL 地址
curl_setopt($ch, CURLOPT_NOBODY, false); //设定是否输出页面内容
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //返回字符串,而非直接输出到屏幕上
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, false); //连接超时时间,设置为 0,则无限等待
curl_setopt($ch, CURLOPT_TIMEOUT, $timeout); //数据传输的最大允许时间超时,设为一小时
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY); //HTTP验证方法
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //不检查 SSL 证书来源
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //不检查 证书中 SSL 加密算法是否存在
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); //跟踪爬取重定向页面
curl_setopt($ch, CURLOPT_AUTOREFERER, true); //当Location:重定向时,自动设置header中的Referer:信息
curl_setopt($ch, CURLOPT_ENCODING, &#39;&#39;); //解决网页乱码问题
curl_setopt($ch, CURLOPT_REFERER, $_SERVER[&#39;HTTP_REFERER&#39;]);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER[&#39;HTTP_USER_AGENT&#39;]);
$httpheaders = array();
$httpheaders[] = &#34;CLIENT-IP: {$_SERVER[&#39;HTTP_CLIENT_IP&#39;]}&#34;;
$httpheaders[] = &#34;X-FORWARDED-FOR: {$_SERVER[&#39;HTTP_X_FORWARDED_FOR&#39;]}&#34;;
curl_setopt($ch, CURLOPT_HTTPHEADER, $httpheaders);
$data = curl_exec($ch); //运行 curl,请求网页并返回结果
curl_close($ch); //关闭 curl
return $data;
}
?> |
|