• PHP模拟浏览器请求(解决抓取反爬网站问题)


    class Request
    {
        private $curl;
        private $query = '';
        private $user_agent = 'Mozilla/5.0 (Linux; Android 6.0; Nexus 5 Build/MRA58N) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.25 Mobile Safari/537.36';
    
        public function __construct()
        {
            $this->curl = curl_init(); // 初始化
        }
    
        public function request($url, $data = [], $type = 'GET')
        {
            $ret = '';
    
            switch ($type) {
    
                case 'GET';
                    $ret = $this->get($url, $data);
                    break;
    
                case 'POST';
                    $ret = $this->post($url, $data);
                    break;
    
            }
    
            return $ret;
        }
    
        // 模拟浏览器get请求
        public function get($url, $data)
        {
            if ($data) {
                foreach ($data as $key => $value) {
                    $this->query .= $key . '=' . $value;
                }
    
                $url .= '?' . $this->query;
            }
    
            curl_setopt($this->curl, CURLOPT_PROXY, $GLOBALS ['proxy']);//代理服务器地址
            curl_setopt($this->curl, CURLOPT_URL, $url); // 要访问的地址
            curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
            curl_setopt($this->curl, CURLOPT_SSL_VERIFYHOST, 2); // 从证书中检查SSL加密算法是否存在
            curl_setopt($this->curl, CURLOPT_USERAGENT, $this->user_agent); // 模拟用户使用的浏览器
            @curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
            curl_setopt($this->curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
            curl_setopt($this->curl, CURLOPT_HTTPGET, 1); // 发送一个常规的Post请求
            curl_setopt($this->curl, CURLOPT_COOKIEFILE, $GLOBALS ['cookie_file']); // 读取上面所储存的Cookie信息
            curl_setopt($this->curl, CURLOPT_TIMEOUT, 120); // 设置超时限制防止死循环
            curl_setopt($this->curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
            curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
            $tmpInfo = curl_exec($this->curl); // 执行操作
    
            return $tmpInfo;
        }
    
        // 模拟浏览器post请求
        public function post($url, $data)
        {
            //代理服务器地址
            curl_setopt($this->curl, CURLOPT_PROXY, $GLOBALS ['proxy']);
    
            curl_setopt($this->curl, CURLOPT_URL, $url); // 要访问的地址
            curl_setopt($this->curl, CURLOPT_SSL_VERIFYPEER, 0); // 对认证证书来源的检查
            curl_setopt($this->curl, CURLOPT_SSL_VERIFYHOST, 2); // 从证书中检查SSL加密算法是否存在
            curl_setopt($this->curl, CURLOPT_USERAGENT, $this->user_agent); // 模拟用户使用的浏览器
            @curl_setopt($this->curl, CURLOPT_FOLLOWLOCATION, 1); // 使用自动跳转
            curl_setopt($this->curl, CURLOPT_AUTOREFERER, 1); // 自动设置Referer
            curl_setopt($this->curl, CURLOPT_POST, 1); // 发送一个常规的Post请求
            curl_setopt($this->curl, CURLOPT_POSTFIELDS, $data); // Post提交的数据包
            curl_setopt($this->curl, CURLOPT_COOKIEFILE, $GLOBALS ['cookie_file']); // 读取上面所储存的Cookie信息
            curl_setopt($this->curl, CURLOPT_TIMEOUT, 120); // 设置超时限制防止死循环
            curl_setopt($this->curl, CURLOPT_HEADER, 0); // 显示返回的Header区域内容
            curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1); // 获取的信息以文件流的形式返回
            $tmpInfo = curl_exec($this->curl); // 执行操作
    
            return $tmpInfo;
        }
    
        public function __destruct()
        {
            //关闭URL请求
            curl_close($this->curl);
        }
    }
    
    
    
    
    $request = new Request();
    
    echo ($request->get('https://wwwtest.fedex.com/document/v1/cache/retrieve/SH,0d2ad8e2dbe1b888794634781559_Merge?isLabel=true&autoPrint=false&retrievalMode=THERMAL',''));
    exit;
  • 相关阅读:
    问题python3中的tablib库报错“'Dataset' object has no attribute 'xlsx' tablib”
    下载并运行 Jenkins
    mac上卸载jdk
    Macbook pro 切换显卡
    Mac 安装和使用MongoDB
    PEP8 规范
    完全卸载Xcode
    mysql重置密码
    安装vue mac 不成功
    error:Cannot pull with rebase
  • 原文地址:https://www.cnblogs.com/jinshao/p/16261911.html
Copyright © 2020-2023  润新知