{"id":425,"title":"php curl\u6293https\u7f51\u5740\u65f6301\u6216302\u6216\u6293\u53d6\u5185\u5bb9\u4e3a\u7a7a\u65f6\u7684\u5904\u7406\u65b9\u6cd5","good":0,"bad":0,"hit":4124,"created_at":"2019-03-22 17:02:33","content":"

\u573a\u666f\uff1a\u68c0\u6d4b\u7f51\u7ad9\u662f\u5426\u5b89\u88c5\u4e86\u67d0\u6bb5\u4ee3\u7801<\/p>

<\/p>

\u7528curl\u6293\u9996\u9875\u4ee3\u7801\u8fdb\u884c\u5206\u6790\uff0c\u4e00\u822chttp\u7f51\u5740\u662f\u6ca1\u95ee\u9898\uff0c\u4f46\u6709\u4e9b\u7f51\u7ad9\u505a\u4e86http\u81ea\u52a8\u8df3\u8f6c\u5230https\u7684\u8bbe\u7f6e\uff0c\u8fd9\u65f6\u53bb\u6293\u5c31\u4f1a\u8fd4\u56de301\u6216302\u4e4b\u7c7b\u7684\u9519\u8bef\uff0c\u5e76\u4e0d\u80fd\u6293\u5230\u9996\u9875\u4ee3\u7801<\/p>

<\/p>

https\u6709\u4e9b\u7f51\u7ad9\u662f\u9700\u8981\u5e26User-Agent\u53cacookie\u624d\u80fd\u6293\u5230\u7684\uff0c\u90a3\u8981\u5982\u4f55\u53bb\u83b7\u5f97cookie\u5462\uff1b\u7ecf\u8fc7\u4e00\u6bb5\u65f6\u95f4\u6298\u817e\uff0c\u6700\u7ec8\u5b9e\u73b0\uff0c\u601d\u8def\uff1a<\/p>

\u5148curl\u8bf7\u6c42\u4e00\u6b21\u53d6\u5f97cookie,\u7136\u540e\u518d\u5c06\u7b2c\u4e00\u6b21\u83b7\u5f97\u7684cookie\u5f04\u51fa\u6765\uff0c\u5c06\u8fd9\u4e2acookie\u653e\u5728header\u4e2d\u518d\u8bf7\u6c42\u4e00\u6b21\u5c31\u53ef\u4ee5\u4e86\uff1b <\/p>

\u7b2c\u4e00\u6b21\u7528\u7684\uff1aget_headers\u53bb\u53d6cookie\u5728windows\u73af\u5883\u4e0b\u53ef\u4ee5\uff0c\u653e\u5230\u7ebf\u4e0a\u4e0d\u884c\uff0c\u540e\u6765\u5c31\u6539\u7528curl\u4e86<\/p>

<\/p>

            \/\/\u5934\u5904\u7406<\/p>

            $header = [<\/p>

                'User-Agent:Mozilla\/5.0 (Windows NT 10.0; WOW64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/70.0.3538.102 Safari\/537.36',<\/p>

                'Accept:text\/html,application\/xhtml+xml,application\/xml;q=0.9,image\/webp,image\/apng,*\/*;q=0.8',<\/p>

                'Accept-Encoding:gzip, deflate, br',<\/p>

                'Accept-Language:zh-CN,zh;q=0.9',<\/p>

            ];<\/p>

            \/\/http<\/p>

            $output = $this->_webCurl($domain, $header);<\/p>

            if ($output) {<\/p>

                if (preg_match_all($str, $output) >= 1) {<\/p>

                    $result = 1;<\/p>

                }<\/p>

            }<\/p>

            \/\/https<\/p>

            if (!$result) {<\/p>

                \/\/\u5934\u5904\u7406-linux\u73af\u5883\u5931\u8d25<\/p>

\/\/                $headerArr = get_headers($httpsDomain, 1);<\/p>

\/\/                if (!$headerArr) {<\/p>

\/\/                    responseError(600, '\u8bf7\u6c42\u5931\u8d25,\u8bf7\u91cd\u8bd5');<\/p>

\/\/                }<\/p>

\/\/                if ($headerArr && isset($headerArr['Set-Cookie'])) {<\/p>

\/\/                    if (is_array($headerArr['Set-Cookie'])) {<\/p>

\/\/                        $cookie = $headerArr['Set-Cookie'][0];<\/p>

\/\/                    } else {<\/p>

\/\/                        $cookie = $headerArr['Set-Cookie'];<\/p>

\/\/                    }<\/p>

\/\/                } else {<\/p>

\/\/                    $cookie = '';<\/p>

\/\/                }<\/p>

                $content = $this->_webCurl($httpsDomain, $header, 1);<\/p>

                $pattern = '\/Set-Cookie: .+\/';<\/p>

                preg_match($pattern, $content, $matches);<\/p>

                if (isset($matches[0])) {<\/p>

                    $cookie = substr($matches[0], 12);<\/p>

                } else {<\/p>

                    $cookie = '';<\/p>

                }<\/p>

                array_push($header, "Cookie:$cookie");<\/p>

                $output = $this->_webCurl1($httpsDomain, [], '', $cookie);<\/p>

                if ($output) {<\/p>

                    if (preg_match_all($str, $output) >= 1) {<\/p>

                        $result = 1;\/\/\u8868\u793a\u5df2\u5b89\u88c5\u4ee3\u7801\uff0c\u539f\u521b\u4e0d\u6613\uff0c\u8f6c\u8f7d\u5c31\u4fdd\u7559http:\/\/www.hu-rong.com<\/a> <\/p>

                    }<\/p>

                }<\/p>

            }<\/p>

<\/p>

<\/p>

<\/p>

<\/p>

    \/**<\/p>

     * \u4e13\u95e8\u6293\u7ad9\u70b9\u4ee3\u7801\u7528\u7684<\/p>

     *<\/p>

     * @param  string $url \u5b8c\u6574\u7684url<\/p>

     * @param $header<\/p>

     * @param int $responseHeader \u662f\u5426\u8f93\u51faheader\u4fe1\u606f<\/p>

     * @return bool|string<\/p>

     *\/<\/p>

    private function _webCurl($url, $header, $responseHeader = 0)<\/p>

    {<\/p>

        $curl = curl_init();<\/p>

        \/\/ \u8bbe\u7f6e\u9009\u9879\uff0c\u5305\u62ecURL<\/p>

        curl_setopt($curl, CURLOPT_URL, $url);<\/p>

        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);<\/p>

        curl_setopt($curl, CURLOPT_HEADER, $responseHeader);\/\/\u662f\u5426\u8f93\u51faheader\u4fe1\u606f<\/p>

<\/p>

        curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);<\/p>

        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false); \/\/ \u8df3\u8fc7\u8bc1\u4e66\u68c0\u67e5<\/p>

        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);  \/\/ \u4ece\u8bc1\u4e66\u4e2d\u68c0\u67e5SSL\u52a0\u5bc6\u7b97\u6cd5\u662f\u5426\u5b58\u5728<\/p>

        \/\/curl_setopt($curl, CURLOPT_HTTPHEADER, $header);\u8fd9\u79cd\u6d4b\u8bd5\u5c45\u7136\u4e0d\u884c\uff0c\u6539\u6210\u4e0b\u9762\u8fd9\u884c<\/strong><\/p>

            curl_setopt($curl, CURLOPT_HTTPHEADER, array('Accept-Encoding: gzip, deflate'));<\/p>

        curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate,br');\/\/\u89e3\u6790gzip,deflate\u5185\u5bb9<\/p>

        \/\/curl_setopt($curl, CURLOPT_USERAGENT, 'Mozilla\/5.0 (Windows NT 10.0; WOW64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/72.0.3608.4 Safari\/537.36');<\/p>

        \/\/ \u6267\u884c\u5e76\u83b7\u53d6HTML\u6587\u6863\u5185\u5bb9<\/p>

        $output = curl_exec($curl);<\/p>

        if ($output === FALSE) {<\/p>

            \/\/echo "CURL Error:" . curl_error($curl);<\/p>

        }<\/p>

        \/\/ 4\u91ca\u653ecurl\u53e5\u67c4<\/p>

        curl_close($curl);<\/p>

<\/p>

        return $output;<\/p>

    }<\/p>

<\/p>

<\/p>

\u4e0a\u8ff0\u65b9\u6cd5\u5728\u67d0\u4e9b\u60c5\u51b5\u4e0b\u8fd8\u662f\u4f1a\u5931\u8d25\uff0c\u6700\u8fd1\u4f18\u5316\u7684<\/p>

$content = $this->_webCurl($httpsDomain, $header, 1);\n$pattern = '\/Set-Cookie: .+\/';\npreg_match($pattern, $content, $matches);\nif (isset($matches[0])) {\n    $cookie = substr($matches[0], 12);\n} else {\n    $cookie = '';\n}\n\n$output=$this->_webCurl1($httpsDomain,[],'',$cookie);\n\nprivate function _webCurl1($url, $data, $ref, $cookie)\n{ \/\/ \u6a21\u62df\u63d0\u4ea4\u6570\u636e\u51fd\u6570\n    $curl = curl_init(); \/\/ \u542f\u52a8\u4e00\u4e2aCURL\u4f1a\u8bdd\n    curl_setopt($curl, CURLOPT_URL, $url); \/\/ \u8981\u8bbf\u95ee\u7684\u5730\u5740\n    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); \/\/ \u5bf9\u8ba4\u8bc1\u8bc1\u4e66\u6765\u6e90\u7684\u68c0\u67e5\n    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2); \/\/ \u4ece\u8bc1\u4e66\u4e2d\u68c0\u67e5SSL\u52a0\u5bc6\u7b97\u6cd5\u662f\u5426\u5b58\u5728\n    curl_setopt($curl, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']); \/\/ \u6a21\u62df\u7528\u6237\u4f7f\u7528\u7684\u6d4f\u89c8\u5668\n    curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1); \/\/ \u4f7f\u7528\u81ea\u52a8\u8df3\u8f6c\n    curl_setopt($curl, CURLOPT_REFERER, $ref);\n    curl_setopt($curl, CURLOPT_POST, 1); \/\/ \u53d1\u9001\u4e00\u4e2a\u5e38\u89c4\u7684Post\u8bf7\u6c42\n    curl_setopt($curl, CURLOPT_POSTFIELDS, $data); \/\/ Post\u63d0\u4ea4\u7684\u6570\u636e\u5305\n    curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie); \/\/ \u8bfb\u53d6\u4e0a\u9762\u6240\u50a8\u5b58\u7684Cookie\u4fe1\u606f\n    curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie); \/\/ \u5b58\u653eCookie\u4fe1\u606f\u7684\u6587\u4ef6\u540d\u79f0\n\n    curl_setopt($curl, CURLOPT_HTTPHEADER, array('Accept-Encoding: gzip, deflate'));\n    curl_setopt($curl, CURLOPT_ENCODING, 'gzip,deflate');\n    curl_setopt($curl, CURLOPT_TIMEOUT, 30); \/\/ \u8bbe\u7f6e\u8d85\u65f6\u9650\u5236\u9632\u6b62\u6b7b\u5faa\u73af\n    curl_setopt($curl, CURLOPT_HEADER, 0); \/\/ \u663e\u793a\u8fd4\u56de\u7684Header\u533a\u57df\u5185\u5bb9\n    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); \/\/ \u83b7\u53d6\u7684\u4fe1\u606f\u4ee5\u6587\u4ef6\u6d41\u7684\u5f62\u5f0f\u8fd4\u56de\n    $tmpInfo = curl_exec($curl); \/\/ \u6267\u884c\u64cd\u4f5c\n    if (curl_errno($curl)) {\n        echo 'Errno' . curl_error($curl);\n    }\n    curl_close($curl); \/\/ \u5173\u952eCURL\u4f1a\u8bdd\n    return $tmpInfo; \/\/ \u8fd4\u56de\u6570\u636e\n}<\/pre>

<\/p>"}