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){
$response = $this->resultFactory
->create(MagentoFrameworkControllerResultFactory::TYPE_JSON)
->setData([
'status' => "sucess", 'response' => 'sucess']);
return $response;
}
else{
$response = $this->resultFactory
->create(MagentoFrameworkControllerResultFactory::TYPE_JSON)
->setData([
'status' => "fail", 'response' => $code.' My File Error '. $url]);
return $response;
}