I found a very nice class, WP_Http, in the WordPress core to grab external web page contents.
The problem is that it throws a PHP error when the url is not found, Fatal error: Cannot use object of type WP_Error as array.
$url = 'http://asklsahgioa.fdjeoiao.griawpo'; // non-existent url
$oHttp = new WP_Http;
$output = $oHttp->request($url);
print_r($output['body']);
So I'd like to make sure if the url exists prior to the class method. The following code works but it's slow and I can see the response takes for a second or so when the url is found. That means it doulbes the speed down if the url is fine.
$handle = @fopen($url,'r');
if($handle !== false)
echo 'true';
else
echo 'false';
I tried get_headers()
as well; however, it also takes for a second when the url is found.
$siteHeader = @get_headers($url , 1);
if ($siteHeader > 1)
echo 'true';
else
echo 'false';
So, is there a better way of doing this? Maybe WordPress already has a function for it or extend the class to embed the error handling?