How can PHP handle HTTP request failures, such as receiving a "403 Forbidden" error?
When PHP receives a "403 Forbidden" error during an HTTP request, it means that the server is refusing to fulfill the request due to insufficient permissions. To handle this error in PHP, you can catch the exception using a try-catch block and then handle it accordingly, such as displaying an error message to the user or redirecting them to a different page.
try {
$response = file_get_contents('http://example.com/api/data');
// Process the response
} catch (Exception $e) {
if ($e->getCode() == 403) {
echo "403 Forbidden: You do not have permission to access this resource.";
} else {
echo "An error occurred: " . $e->getMessage();
}
}