1

如何使用 PHP 找出我在 Google 上的关键字位置?

我试过这个网址:

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

但我无法获得任何 html 源代码。

我怎样才能做到这一点 ?

4

2 回答 2

2

我使用我制作的这个脚本。因为它依赖于可能从谷歌更改 html,所以它不可靠,但它现在可以工作:

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

?>
于 2016-07-11T13:15:25.893 回答
0

也许您应该只使用为此目的编写的类,例如github 上的这个

于 2013-09-29T16:33:52.670 回答