Curl请求适用于控制台,但不适用于PHP

时间:2017-04-29 08:32:38

标签: php curl

这个简单的请求适用于我的控制台

curl 'https://www.nike.com/en'

但是在PHP中(有或没有像header,useragent等选项)我得到Access Denied 403

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.nike.com/en");
$result = curl_exec($ch);

由于

2 个答案:

答案 0 :(得分:0)

嗯,这对你有用。

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, "http://www.nike.com/en");
    curl_setopt($ch, CURLOPT_COOKIEJAR, "cookie.txt");
    curl_setopt($ch, CURLOPT_COOKIEFILE, "cookie.txt");
    curl_setopt($ch, CURLOPT_NOBODY, false);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_TIMEOUT, 40000);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$result = curl_exec($ch);
echo $result;

答案 1 :(得分:0)

<?php

class Curl {

    public static function makeRequest($url) {
            $ch = curl_init();
            $request_headers = [
                'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8;',
                'Accept-Encoding: gzip, deflate',
                "Connection: keep-alive",
                "Content-Type: text/html; charset=UTF-8",
            ];
            $options = [
                CURLOPT_URL => $url,
                CURLOPT_CONNECTTIMEOUT => 30,
                CURLOPT_RETURNTRANSFER => true,
                CURLOPT_FOLLOWLOCATION => true,
                CURLOPT_USERAGENT => "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:53.0) Gecko/20100101 Firefox/53.0",
                CURLOPT_SSL_VERIFYHOST => false,
                CURLOPT_SSL_VERIFYPEER => false,
                CURLOPT_AUTOREFERER => true,
                CURLOPT_COOKIESESSION => true,
                CURLOPT_FILETIME => true,
                CURLOPT_FRESH_CONNECT => true,
                CURLOPT_HTTPHEADER => $request_headers,
                CURLOPT_COOKIESESSION => true,
                CURLOPT_ENCODING => "gzip, deflate, scdh",
            ];
            curl_setopt_array($ch, $options);
            $result['content'] = curl_exec($ch);
            $result['header'] = curl_getinfo($ch);
            $result['error'] = curl_error($ch);
            return $result;
    }
}

var_dump(Curl::makeRequest('https://www.nike.com/en')['header']);

有些网站会严格检查请求标头并返回您编码的内容。我添加了请求标头广告解码方法来解码内容