How can I check if a URL exists in PHP, especially when accessing it through a proxy?

To check if a URL exists in PHP, especially when accessing it through a proxy, you can use the cURL library to make a request to the URL and check the response code. If the response code is in the 200 range, it means the URL exists. You can also set the proxy settings in the cURL request to access the URL through a proxy.

$url = 'http://example.com';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_PROXY, 'proxy_ip:proxy_port'); // Set proxy settings if needed

$response = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

if ($httpCode >= 200 && $httpCode < 300) {
    echo 'URL exists';
} else {
    echo 'URL does not exist';
}

curl_close($ch);