• dnspod CURL模拟访问


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

    <?php
    
    class 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("
    
    ", $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;
        }
    }

    欢迎转载,转载请注明:转载自[ http://www.cnblogs.com/zjfree/ ]
  • 相关阅读:
    [Docker]一键部署gitlab中文版
    [Docker]python 2.7.5 docker-compose安装
    [CentOS7]pip安装
    快速傅里叶变换FFT
    HDU 4734 f(x)
    DP
    HDU 3555 Bomb
    HDU 5898 odd-even number
    将文本拷贝到剪贴板
    数论分块
  • 原文地址:https://www.cnblogs.com/zjfree/p/14344373.html
Copyright © 2020-2023  润新知