简体   繁体   中英

Getting curl response from CLI but access denied from PHP

For many requests I am getting access denied errors when using curl from php, my code is:

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
    curl_setopt($ch, CURLOPT_URL, 'https://www.vueling.com/en');
    curl_setopt($ch, CURLOPT_TIMEOUT, '2');
    curl_setopt($ch, CURLOPT_VERBOSE, 1);
    $data = curl_exec($ch);
    curl_close($ch);
    echo $data;

which returns this:

<H1>Access Denied</H1>

However if I run it directly on the command line:

curl -v https://www.vueling.com/en

I get the full response, my ip is not blocked, im not sure why CLI works but PHP doesnt? Anyone have any ideas?

The CLI version of curl probably sends an user agent that is accepted by this website, but the PHP version of curl may send an user agent that is rejected.

So you have to check that the headers sent by curl are similar in both ways.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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