Search code examples
phpcurlcloudflare

PHP Curl accessing file always show 403 error


I am trying to access a JSON file through Curl but it always shows a 403 forbidden error.

I am using Cloudflare on my site.

Here is the code, only else block is running.

I can access this JSON through browser and FTP there is no error but through Curl and wget it shows 403.

$url = "https://example.com/pub/media/sample.json";
            
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);

curl_setopt($ch, CURLOPT_USERAGENT, 'User-Agent: curl/7.39.0');

$result = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

curl_close($ch);

if ($code == 200) {
    $status = true;
} else {
    $status = false;
}
curl_close($ch);
        
if ($status){
    echo 'sucess';
}
else{
    echo  $code.' My File Error '. $url;
    
}

Solution

  • This fixed my issue.

    I was using Curl on my server to access files but it was not allowing Curl or WGET to access any file and Cloudflare was blocking all requests.

    I went to Cloudflare and whitelisted the my server own IP in

    Security -> Web Application Firewall (WAF)