Skip to content

Instantly share code, notes, and snippets.

@qsun
Created January 21, 2012 23:00
Show Gist options
  • Save qsun/1654432 to your computer and use it in GitHub Desktop.
Save qsun/1654432 to your computer and use it in GitHub Desktop.
Check if URL is indexed by Google
<?php
function indexed($url) {
$url = 'http://webcache.googleusercontent.com/search?q=cache:' . urlencode($url);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
curl_setopt($ch, CURLOPT_NOBODY, true);
curl_setopt($ch, CURLOPT_USERAGENT, 'Chrome 10');
if (!curl_exec($ch)) {
// var_dump('failed');
return false;
}
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
// var_dump($code);
return $code == '200';
}
@dryleaf
Copy link

dryleaf commented Aug 22, 2019

curl -I --user-agent 'Chrome 10' http://webcache.googleusercontent.com/search?q=cache:<your url here>

@yuis-ice
Copy link

yuis-ice commented Oct 1, 2020

check_if_url_google_index(){

	local url="${1}"
	local code
	code="$( curl --user-agent 'Chrome 10' -s -o /dev/null -w "%{http_code}" 'https://webcache.googleusercontent.com/search?q=cache:'"${url}" )"

	[[ "${code}" == "200" ]] && return 0 || return 1
}

check_if_url_google_index "https://flippa.com/8812637-thefamousfolk-com" && echo "yes" || echo "no"
check_if_url_google_index "https://flippa.com/foobar.tld" && echo "yes" || echo "no"

@kukhanh29
Copy link

kukhanh29 commented Jul 22, 2021

function indexed($url) {
    $url = 'https://webcache.googleusercontent.com/search?q=cache:' . urlencode($url);
    
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, false);
    curl_setopt($ch, CURLOPT_NOBODY, false);
    curl_setopt( $ch, CURLOPT_RETURNTRANSFER, 1) ;
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt( $ch, CURLOPT_HTTPHEADER, array(
        "Content-Type: application/json; charset=UTF-8"
    )) ;
    //curl_setopt($ch, CURLOPT_USERAGENT, 'Chrome 10');
    curl_exec($ch);
    $code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    //var_dump($code);
    curl_close($ch);

    return $code;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment