Emre Kadan
Emre Kadan

Reputation: 49

how to find out keyword position on google with php

How can I find out my keyword position on Google with PHP ?

I tried this URL :

http://www.google.com/url?sa=t&source=web&ct=res&cd=7&url=http%3A%2F%2Fwww.example.com%2Fmypage.htm&ei=0SjdSa-1N5O8M_qW8dQN&rct=j&q=flowers&usg=AFQjCNHJXSUh7Vw7oubPaO3tZOzz-F-u_w&sig2=X8uCFh6IoPtnwmvGMULQfw

but I couldn't get any html source code.

How can I do this ?

Upvotes: 2

Views: 10050

Answers (2)

Oli
Oli

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

Jaroslav
Jaroslav

Reputation: 1389

Maybe you should just use a class written for this purpose, for example this one on github

Upvotes: 0

Related Questions