简体   繁体   English

卷曲请求可在控制台上使用,但不适用于PHP

[英]Curl request works on console but not in PHP

This simple request works on my console 这个简单的请求可以在我的控制台上运行

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

But in PHP (with or without options like headers, useragent ...) I get Access Denied 403 但是在PHP中(带有或不带有标题,useragent等选项),我得到Access Denied 403

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

Thanks 谢谢

Well, this would work for you. 好吧,这将为您工作。

    $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;
<?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']);

Some website strictly check the request headers and return you encoded so .. I added request headers ad decoding method to decode the content 一些网站严格检查请求标头并返回给您编码,..我添加了请求标头广告解码方法来解码内容

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM