8

How to deal with the forbidden error CURL 403? Solutions?

 3 years ago
source link: https://www.codesd.com/item/how-to-deal-with-the-forbidden-error-curl-403-solutions.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

How to deal with the forbidden error CURL 403? Solutions?

advertisements

I am using multi curl to fetch data from remote site. My script is like

foreach ($urls as $i => $url) {
            $ch[$i] = curl_init($url['url']);
            curl_setopt($ch[$i], CURLOPT_TIMEOUT, 0);
            curl_setopt($ch[$i], CURLOPT_RETURNTRANSFER, 1);
            curl_setopt($ch[$i], CURLOPT_CONNECTTIMEOUT, 0);
            curl_setopt($ch[$i], CURLOPT_SSL_VERIFYPEER, false);
            curl_multi_add_handle($multiCurlHandler, $ch[$i]);
}

It returns me 403 forbidden in response.

Thanks in advance for suggestions and comments.


Just try by adding two lines for User agents, and see if it works or not. Some servers not accept the requests from scripts, it depends on user agents.

// line 1
$agent = 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1; .NET CLR 1.0.3705; .NET CLR 1.1.4322)';

foreach ($urls as $i => $url) {
    $ch[$i] = curl_init($url['url']);
    curl_setopt($ch[$i], CURLOPT_TIMEOUT, 0);
    curl_setopt($ch[$i], CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch[$i], CURLOPT_CONNECTTIMEOUT, 0);

    // line 2
    curl_setopt($ch[$i], CURLOPT_USERAGENT, $agent);

    curl_setopt($ch[$i], CURLOPT_SSL_VERIFYPEER, false);
    curl_multi_add_handle($multiCurlHandler, $ch[$i]);
}


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK