I'm looking to read a URL from one of two servers.
Using CURL I want to see if a URL exists and can be read form server A, if it available read the URL to a variable.
If it's not available from the same on URL b. If that is available read the URL to a variable..
If that isn't available set the variable to ''
This is what I have so far..
$url='http://test.com/ip.php';
$urltwo='http://example.com/ip.php';
$ch = curl_init($url);
$timeout=1;
curl_setopt($ch, CURLOPT_NOBODY, true);
$data = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($httpCode == '200') {
curl_setopt($ch, CURLOPT_NOBODY, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
}
var_dump( $output );
Can some one advise how to do this ? Thanks
I've come up with a possible fix.
<?php
$ip = '';
$server = checkURL('http://servera.com/ip.php');
if ($server) {
$ip = $server;
} else {
$server = checkURL('http://serverbcom/ip.php');
if ($server) $ip = $server;
}
function checkURL($url) {
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 1);
$ch = curl_init($url);
$data = curl_exec($ch);
$httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($httpCode == '200') {
curl_setopt($ch, CURLOPT_NOBODY, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$output = curl_exec($ch);
return $output;
}
}
?>