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?
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));