JSON解析接口套娃 轮询批量解析

如果有个JSON接口需授权IP或域名使用,那就给它套个娃,然后就可随意调用,也可以自定义返回自己想要的内容,废话不多说,代码如下:

单解析

<?PHP

header("content-type:application/json;charset=utf8");

$url = $_GET['url'];
if($_GET['url']==''){
    $arr = [
        'code' => 404,
        'msg' => '缺少URL',
        'from' => $_server['HTTP_HOST']
        ];
    die(json_encode($arr, JSON_UNESCAPED_UNicoDE|JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES));
}

$Api='我是接口放你自己得'; //JSON接口
$jiexi = $Api.$url;
$html = httpget($jiexi);
$json = json_decode($html, true);
$vurl=$json['url'];
if($vurl ==''||$vurl ==null){
    $arr = [
        'code' => 100,
        'msg' => '解析失败',
        'link' => $url,
        'from' => $_SERVER['HTTP_HOST']
        ];
    die(json_encode($arr, JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES));
}
$arr = [
    'code' => 200,
    'msg' => '解析成功',
    'url' => $vurl,
    'link' => $url,
    'from' => $_SERVER['HTTP_HOST']
    ];
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, '');                                 //解决网页乱码问题
    curl_setopt($ch, CURLOPT_REFERER, $_SERVER['HTTP_REFERER']);
    curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    
    $httpheaders = array();
    $httpheaders[] = "CLIENT-IP: {$_SERVER['HTTP_CLIENT_IP']}";
    $httpheaders[] = "X-FORWARDED-FOR: {$_SERVER['HTTP_X_FORWARDED_FOR']}";
    curl_setopt($ch, CURLOPT_HTTPHEADER, $httpheaders);
    
    $data = curl_exec($ch);                                                 //运行 curl,请求网页并返回结果
    curl_close($ch);                                                        //关闭 curl
    return $data;
}

?>

多接口

<?php

//JSON格式化
header("content-type:application/json;charset=utf8");

//当URL为空时返回内容
$url = $_GET['url'];
if($_GET['url'] == '') {
    $arr = [
        'code' => 404,
        'msg' => '缺少URL!',
        'from' => $_SERVER['HTTP_HOST']
        ];
    die(json_encode($arr, JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES));
}

$json1 = ''; //接口1

$json2 = ''; //接口2

$json3 = ''; //接口3

$json4 = ''; //接口4

$json5 = ''; //接口5

//接口轮询
$jiexi = $json1.$url;
$html = httpget($jiexi);
$data = json_decode($html, true);
if ($data['url'] == '' || $data['url'] == null) {
    $jiexi = $json2.$url;
    $html = httpget($jiexi);
    $data = json_decode($html, true);
}
if ($data['url'] == '' || $data['url'] == null) {
    $jiexi = $json3.$url;
    $html = httpget($jiexi);
    $data = json_decode($html, true);
}
if ($data['url'] == '' || $data['url'] == null) {
    $jiexi = $json4.$url;
    $html = httpget($jiexi);
    $data = json_decode($html, true);
}
if ($data['url'] == '' || $data['url'] == null) {
    $jiexi = $json5.$url;
    $html = httpget($jiexi);
    $data = json_decode($html, true);
}
if ($data['url'] == '' || $data['url'] == null) {
    $arr = [
        'code' => 100,
        'msg' => '解析失败',
        'link' => $url,
        'from' => $_SERVER['HTTP_HOST']
        ];
    die(json_encode($arr, JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES));
}

$arr = [
    'code' => 200,
    'msg' => '解析成功',
    'url' => $data['url'],
    'link' => $url,
    'from' => $_SERVER['HTTP_HOST']
    ];
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, '');                                 //解决网页乱码问题
    curl_setopt($ch, CURLOPT_REFERER, $_SERVER['HTTP_REFERER']);
    curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    
    $httpheaders = array();
    $httpheaders[] = "CLIENT-IP: {$_SERVER['HTTP_CLIENT_IP']}";
    $httpheaders[] = "X-FORWARDED-FOR: {$_SERVER['HTTP_X_FORWARDED_FOR']}";
    curl_setopt($ch, CURLOPT_HTTPHEADER, $httpheaders);
    
    $data = curl_exec($ch);                                                 //运行 curl,请求网页并返回结果
    curl_close($ch);                                                        //关闭 curl
    return $data;
}

?>

while循环:

<?php

//JSON格式化
header("content-type:application/json;charset=utf8");

//当URL为空时返回内容
$url = $_GET['url'];
if($_GET['url'] == '') {
    $arr = [
        'code' => 404,
        'msg' => '缺少URL!',
        'from' => $_SERVER['HTTP_HOST']
        ];
    die(json_encode($arr, JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES));
}

$json = array(
    '',
    '',
    '',
    '',
    '',
    ); //接口

//接口轮询
$num = 0;
$jiexi = $json[$num].$url;
$html = httpget($jiexi);
$data = json_decode($html, true);

while ($data['url'] == '' || $data['url'] == null) {
    $jiexi = $json[$num+1].$url;
    $html = httpget($jiexi);
    $data = json_decode($html, true);
    $num = $num + 1;
    if ($num >= count($json)) {
        break;
    }
}

if ($data['url'] == '' || $data['url'] == null) {
    $arr = [
        'code' => 100,
        'msg' => '解析失败',
        'link' => $url,
        'from' => $_SERVER['HTTP_HOST']
        ];
    die(json_encode($arr, JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES));
}
else {
    $arr = [
        'code' => 200,
        'msg' => '解析成功',
        'url' => $data['url'],
        'link' => $url,
        'from' => $_SERVER['HTTP_HOST']
        ];
    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, '');                                 //解决网页乱码问题
    curl_setopt($ch, CURLOPT_REFERER, $_SERVER['HTTP_REFERER']);
    curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    
    $httpheaders = array();
    $httpheaders[] = "CLIENT-IP: {$_SERVER['HTTP_CLIENT_IP']}";
    $httpheaders[] = "X-FORWARDED-FOR: {$_SERVER['HTTP_X_FORWARDED_FOR']}";
    curl_setopt($ch, CURLOPT_HTTPHEADER, $httpheaders);
    
    $data = curl_exec($ch);                                                 //运行 curl,请求网页并返回结果
    curl_close($ch);                                                        //关闭 curl
    return $data;
}

?>

 

版权声明

   站内部分内容来自互联网,该文仅限用于学习和研究目的。本站仅提供网络资源分享服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请 联系我们 一经核实,立即删除。访问和下载本站内容,说明您已同意上述条款。在为用户提供最好的产品同时,保证优秀的服务质量。


本站仅提供信息存储空间,不拥有所有权,不承担相关法律责任。
THE END
喜欢就支持一下吧
点赞0 分享
评论 抢沙发

请登录后发表评论

    暂无评论内容