i am trying to get string of cross domain but sometime or some website gives a 403 Forbidden error. So to protect from getting error i am trying to include if statement, if site one get error while getting string then it will move to else part and take string from site two.
Error :
Warning: file_get_contents(https://www.example.com): Failed to open stream: HTTP request failed! HTTP/1.1 403 Forbidden
Code :
$siteone = file_get_contents("https://www.example.com");
$sitetwo = file_get_contents("https://www.example.net");
if ($siteone === false) {
$error = error_get_last();
echo $sitetwo;
} else {
echo $siteone;
}
So, here if example.com give 403 error then it automatically ignore example.com and get string from example.net.
I have also tried try and catch but it didn’t work. Please help!!