PHP Curl - 在下载之前检查来自URLA或B的响应

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;
    } 
}


?>

May it will help you:

$file_headers = @get_headers($ch);
if(!$file_headers || strpos($headers[0], '404 Not Found'))
    $exists = false;
else $exists = true;