JSON解析接口套娃

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

一个娃:

<?php

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

$url = $_GET['url'];
if($_GET['url']==''){
    $arr = [
        'code' => 404,
        'msg' => '缺少URL',
        'from' => 'api.leafone.cn'
        ];
    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' => 'api.leafone.cn'
        ];
    die(json_encode($arr, JSON_UNESCAPED_UNICODE|JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES));
}
$arr = [
    'code' => 200,
    'msg' => '解析成功',
    'url' => $vurl,
    'link' => $url,
    'from' => 'api.leafone.cn'
    ];
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;
}

?>

 

示例:

图片[1]-JSON解析接口套娃-聆风小站


—— 本页内容已结束,喜欢请分享 ——
© 版权声明
THE END
喜欢就支持一下吧
点赞2 分享
评论 共5条

请登录后发表评论

    • 头像薅一个玩玩0
    • 头像富瑞阿得0
    • 头像橘猫0