阅读 159

dnspod CURL模拟访问

标签:decode   vat   client   code   exp   empty   php   query   format   

使用CURL模拟访问网页,保留返回的COOKIE

<?phpclass dnspod {    public static function api_call($api, $data) {        $api = ‘https://dnsapi.cn/‘ . $api;        echo $api . PHP_EOL;        $data = array_merge($data, [            ‘login_token‘    => TOKEN,
            ‘format‘         => ‘json‘, 
            ‘lang‘           => ‘cn‘, 
            ‘error_on_empty‘ => ‘no‘,
        ]);        $result = self::post_data($api, $data, $_SESSION[‘cookies‘]);        if (!$result) {            return false;
        }        $result = explode("\r\n\r\n", $result);        if (preg_match_all(‘/^Set-Cookie:\s*([^;]*)/mi‘, $result[0], $cookies)) {            foreach ($cookies[1] as $key => $value) {                if (substr($value, 0, 1) == ‘t‘) {                    $_SESSION[‘cookies‘] = $value;
                }
            }
        }        $results = @json_decode($result[1], 1);        if (!is_array($results)) {            return false;
        }        if ($results[‘status‘][‘code‘] != 1) {            return $results[‘status‘][‘message‘];
        }        return $results;
    }    private static function post_data($url, $data, $cookie=‘‘) {        $ch = @curl_init();
        
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_HEADER, 1);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1);
        curl_setopt($ch, CURLOPT_COOKIE, $cookie);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
        curl_setopt($ch, CURLOPT_USERAGENT, ‘DNSPod API PHP Web Client/1.0.0 (i@likexian.com)‘);        $result = curl_exec($ch);
        curl_close($ch);        return $result;
    }
}

 

dnspod CURL模拟访问

标签:decode   vat   client   code   exp   empty   php   query   format   

原文地址:https://www.cnblogs.com/zjfree/p/14344373.html


文章分类
后端
文章标签
版权声明:本站是系统测试站点,无实际运营。本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 XXXXXXo@163.com 举报,一经查实,本站将立刻删除。
相关推荐