Is it possible to check number of Google search results of a certain phrase in a PHP script with Google API? I am curious if it can be done someway. If yes, what should I do?
EDIT
Okay, according to drakin8564 advices it is my function:
function google(String $text) {
// create curl resource
$ch = curl_init();
// set url
curl_setopt($ch, CURLOPT_URL, 'https://www.google.com/search?q="'.$text.'"');
//return the transfer as a string
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
libxml_disable_entity_loader(false);
// $output contains the output string
$output = curl_exec($ch);
$xml = new DOMDocument();
$xml->loadHTMLFile($output);
$string = $xml->getElementById('resultStats');
$string = str_replace('Około ', '', $string);
$string = str_replace(',', '', $string);
$results = intval($string);
if($results>0) {
curl_close($ch);
return true;
} else {
curl_close($ch);
return false;
// close curl resource to free up system resources
}
}
NOTE: "Około" means "About" in Polish, Also I have a warning:
Warning: DOMDocument::loadHTMLFile(): I/O warning : failed to load external entity "<!doctype html><html itemscope="" itemtype="http://schema.org/SearchResultsPage" lang="pl"><head><meta content="text/ in C:\xampp2\htdocs\file.php on line 19
And here is an implementation
if(google("flower") == true){
echo "search results exist";
} else {
echo "results don't exist";
}
if(google("7dsa8dsaza") == true){
echo "search results exist";
} else {
echo "results don't exist";
}
Unfortunately there is a problem because both examples cause "search results exist" output while the results for "7dsa8dsaza" do not exist. How to fix it?