PHP - get_header throws error: Filename cannot be empty

PHP - get_header throws error: Filename cannot be empty

这是我正在使用的完整代码,它在其他一些网站上有效,但在 google 上无效并抛出一个错误:

    $url = 'http://www.google.com';
$sitemap = $url.'/sitemap.xml';
$robots = $url.'/robots.txt'; 
$robotsx = [$sitemap, $robots];
$sitemaps = false;
$robotss = false;

//GET REDIRECTED URL
function getRedirectUrl ($robotsx) {
    stream_context_set_default(array(
        'http' => array(
        'method' => 'HEAD'
    )
));
$headers = get_headers($robotsx, 1);
if (isset($headers['Location'])) {
    return is_array($headers['Location']) ? array_pop($headers['Location']) : $headers['Location'];
}
return false;
}

$finalr = getRedirectUrl($robots);
$finals = getRedirectUrl($sitemap);

// IF THERE IS NO REDIRECTED URL
if ($finalr == null){
    $robotss = $robots;
}
if ($finals == null){
    $sitemaps = $sitemap;
}


// GET RESPONSE CODE
function get_http_response_coder ($url) {
  $headersyy = get_headers($url);
  return substr($headersyy[0], 9, 3);
}
print_r(get_http_response_coder($url));

// CHECK MAIN URL FOR REDIRECT
$xgetheads = get_headers($url, 1);  

// IF THERE IS REDIRECT
if (isset($xgetheads['Location'])) {
    if (isset($finalr)){
                $get_http_response_coder = get_http_response_coder($finalr);
    if ( $get_http_response_coder != 404) {
        echo "Your website have robots file. Your robots file is at ".$finalr;
    }
    else{
        echo "Your website does not have robots file.";
    }
    }
    if ($finalr == null){
                $get_http_response_coder = get_http_response_coder($robotss);
    if ( $get_http_response_coder != 404) {
        echo "Your website have robots file. Your robots file is at ".$robotss;
    }
    else{
        echo "Your website does not have robots file.";
    }
    }       
    if(isset($finals)){
    $get_http_response_codes = get_http_response_coder($finals);


    if ( $get_http_response_codes != 404) {
        echo "Your website have sitemap file. Your sitemap file is at ".$finals;
    }
    else{
        echo "Your website does not have sitemap file.";
    }
    }
    if($finals == null){
    $get_http_response_codes = get_http_response_coder($sitemaps);


    if ( $get_http_response_codes != 404) {
        echo "Your website have sitemap file. Your sitemap file is at ".$sitemaps;
    }
    else{
        echo "Your website does not have sitemap file.";
    }
    }       
}

// IF THERE IS NO REDIRECT
else{
    $get_http_response_coder = get_http_response_coder($robots) ;
    $get_http_response_codes = get_http_response_coder($sitemap);

    if ( $get_http_response_coder != 404) {
        echo "Your website have robots file. Your robots file is at ".$robots;
    }
    else{
        echo "Your website does not have robots file.";
    }
    if ( $get_http_response_codes != 404) {
        echo "Your website have sitemap file. Your sitemap file is at ".$sitemap;
    }
    else{
        echo "Your website does not have sitemap file.";
    }
}  

我遇到错误:

Warning: get_headers(): Filename cannot be empty

但是,当我只打印那部分时,我得到了正确的响应代码。 为什么会发生这种情况,我的代码有什么问题?

使用http://格式的URL:

function get_http_response_coder ($url) {
$headersyy = get_headers($url);
return substr($headersyy[0], 9, 3);
}   
$url="http://localhost/sample/Whosebug/fileresp.txt";  
var_dump(get_http_response_coder($url));