1

This is whole code I'm using which works on some other sites, but on google does not and throws me an error:

    $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.";
    }
}  

I'm getting error:

Warning: get_headers(): Filename cannot be empty

but, when I print only that part, I get correct response code. Why is this happening and what is wrong with my code?

9
  • 4
    What's in $url? It's empty! Does it even exist? Please use error_reporting(E_ALL) and ini_set('display_errors',1); Commented Nov 21, 2016 at 13:30
  • @twicejr, I edited it. Commented Nov 21, 2016 at 13:32
  • When I execute your code it works and outputs 302. Maybe your PHP version behaves different from mine. Commented Nov 21, 2016 at 13:33
  • With PHP 5.6, I'm getting 302 without error while executing only this. But when it is part of other script, I get this error again. I'll post other whole code in question. Commented Nov 21, 2016 at 13:59
  • I get this "string(3) "302" " Commented Nov 21, 2016 at 14:01

2 Answers 2

1

Use the URL in http:// format:

function get_http_response_coder ($url) {
$headersyy = get_headers($url);
return substr($headersyy[0], 9, 3);
}   
$url="http://localhost/sample/stackoverflow/fileresp.txt";  
var_dump(get_http_response_coder($url));
Sign up to request clarification or add additional context in comments.

2 Comments

@Vijaya Vignesh Kumar Can you explain why?
I believe he is saying to use url without www
0

I got the same error today:

get_header throws error: Filename cannot be empty

The issue for me was because the URL itself doesn't exist. Once I fixed the link, the issue was resolved.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.