How to deal with CURL 403 forbidden error ? any solutions?

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]);
}

Try setting the Referer and User-Agent headers, but!

... setting CURLOPT_SSL_VERIFYPEER to false is dangerous, and should never be done in production. Wrap that line in a is_dev check of some kind at a minimum.

You are defeating the purpose of SSL by doing so, allowing an attacker to intercept your request and return what they like without verifying their SSL certificate.


Contact whomever runs the site you are trying to connect to and ask them why your request is being forbidden.

You can then either stop (if what you are doing is simply deemed unacceptable by them) or change the request so that it conforms to their rules.

Tags:

Php

Curl