Reputation: 49
How can I find out my keyword position on Google with PHP ?
I tried this URL :
but I couldn't get any html source code.
How can I do this ?
Upvotes: 2
Views: 10050
Reputation: 1692
I use this script I made. As it relies on possibly changing html from google it is not reliable but does it's job for now :
<?php
// Include the phpQuery library
// Download at http://code.google.com/p/phpquery/
include("phpQuery-onefile.php");
$country = "en";
$domain = "stackoverflow.com";
$keywords = "php google keyword rank checker";
$firstnresults = 50;
$rank = 0;
$urls = Array();
$pages = ceil($firstnresults / 10);
for($p = 0; $p < $pages; $p++){
$start = $p * 10;
$baseurl = "https://www.google.com/search?hl=".$country."&output=search&start=".$start."&q=".urlencode($keywords);
$html = file_get_contents($baseurl);
$doc = phpQuery::newDocument($html);
foreach($doc['#ires cite'] as $node){
$rank++;
$url = $node->nodeValue;
$urls[] = "[".$rank."] => ".$url;
if(stripos($url, $domain) !== false){
break(2);
}
}
}
print "Country: ".$country."\n";
print "Domain: ".$domain."\n";
print "Keywords: ".$keywords."\n";
print "Rank: ".$rank."\n";
print "First urls:\n";
print implode("\n", $urls)."\n";
?>
Upvotes: 3
Reputation: 1389
Maybe you should just use a class written for this purpose, for example this one on github
Upvotes: 0