好东西分享 2022-01-04 13:06 采纳率: 100%
浏览 34
已结题

获取到API的结果截取想要的内容显示到页面

获取到API的结果截取想要的内容显示到页面

<?php
header("Content-type: text/html; charset=utf-8"); //解决中文乱码
$domain = $_GET['domain'];
    function request_post($url = '', $post_data = array()) {
        if (empty($url) || empty($post_data)) {
            return false;
        }
        
        $postUrl = $url;
        $curlPost = $post_data;
        $ch = curl_init();//初始化curl
        curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定网页
        curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
        curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
        curl_setopt($ch, CURLOPT_ENCODING, "");//解压
        curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //不验证证书下同
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //不验证证书下同
        $data = curl_exec($ch);//运行curl
        curl_close($ch);
        return $data;
    }
    
    function testAction(){
        $url = 'http://testapi.nabla.cn:8080/domain/whois';
        $post_data['member_id']       = '380709';
        $post_data['timestamp']      = '1641190236';
        $post_data['signature'] = 'a985d8ccbb71ecdf716f02ae2b0749ca';
        $post_data['keyword']    = $_GET['domain'];
        //$post_data = array();
        $res = request_post($url, $post_data);
        echo $res;}
        testAction();
?>

在浏览器输入地址文件.php?domain=域名+后缀(如baidu.com)

我想要达到的结果

img

  • 写回答

2条回答 默认 最新

  • CSDN专家-showbo 2022-01-04 13:45
    关注

    php有json_decode,将字符串转对象就可以获取了,在data数组节点下。不过这个接口返回的数组顺序和标志不同注册商内容不一样。。这个有点难搞。只能通过前缀关键字来获取才行了。代码如下,如果出现未找到关键字,题主需要打印内容出来,然后增加关键字前缀

    img

    <?php
    header("Content-type: text/html; charset=utf-8"); //解决中文乱码
    $domain = $_GET['domain'];
        function request_post($url = '', $post_data = array()) {
            if (empty($url) || empty($post_data)) {
                return false;
            }
            $postUrl = $url;
            $curlPost = $post_data;
            $ch = curl_init();//初始化curl
            curl_setopt($ch, CURLOPT_URL,$postUrl);//抓取指定网页
            curl_setopt($ch, CURLOPT_HEADER, 0);//设置header
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//要求结果为字符串且输出到屏幕上
            curl_setopt($ch, CURLOPT_POST, 1);//post提交方式
            curl_setopt($ch, CURLOPT_ENCODING, "");//解压
            curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
            curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //不验证证书下同
            curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); //不验证证书下同
            $data = curl_exec($ch);//运行curl
            curl_close($ch);
            return $data;
        }
        function testAction($domain){
            $url = 'http://testapi.nabla.cn:8080/domain/whois';
            $post_data['member_id']       = '380709';
            $post_data['timestamp']      = '1641190236';
            $post_data['signature'] = 'a985d8ccbb71ecdf716f02ae2b0749ca';
            $post_data['keyword']    = $domain;
            //$post_data = array();
            $res = request_post($url, $post_data);
            return $res;
            
       }
       $domain=$_GET['domain'];
       $res=testAction($domain);
    
       $obj=json_decode($res,true);
    
    
       //$data 域名查询返回的结果
       //$keywords 数据项前缀关键字数组,不同注册商接口返回的数据并不是固定在对应的数组项中,而且前缀不固定
       //如果返回找不到关键字,需要打印出$res检查关键字前缀,然后增加新的关键字
       function getData($data,$keywords){
          foreach($data as $s){
            foreach($keywords as $kw){
                if(stripos($s,$kw)===0){
                    return str_replace($kw,'',$s);
                }
            }
          }
          return '找不到匹配关键字数据';
       }
    
       $data=$obj['data'];
    ?>
    <!doctype html>
    <meta charset="utf-8">
    <style>
    .good{border-collapse:collapse;}
    .good td{border:solid 1px #ccc;padding:5px}
    </style>
    <table class="good" cellpadding="0" cellspacing="0">
    <tr><td colspan="2"><?php echo $domain; ?></td></tr>
    <tr><td>注册人:<br>Registrar:</td><td><?php echo getData($data,array('Registrant:','Registrar URL:')); ?></td></tr>
    <tr><td>域名状态:<br>Domain Status</td><td><?php echo getData($data,array('Domain Status:')); ?></td></tr>
    <tr><td>注册商:<br>Sponsoring Registrar</td><td><?php echo getData($data,array('Registrar:')); ?></td></tr>
    <tr><td>DNS服务器:<br>Name Server</td><td><?php echo getData($data,array('Name Server:'));  ?></td></tr>
    <tr><td>注册日期:<br>Registration Date(EDT)</td><td><?php echo getData($data,array('Registration Time:','Creation Date:'));  ?></td></tr>
    <tr><td>过期日期:<br>Expiration Data(EDT)</td><td><?php echo getData($data,array('Expiration Time:','Registrar Registration Expiration Date:'));  ?></td></tr>
    </table>
     
    
    
    

    img

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论 编辑记录
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 1月12日
  • 已采纳回答 1月4日
  • 创建了问题 1月4日

悬赏问题

  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)