始终返回0,Google SERP Checker

时间:2011-02-28 11:10:25

标签: php html

我的功能搜索谷歌搜索特定的关键字,然后检查网站,然后返回它在谷歌(它为我的搜索引擎板)的位置,但它总是返回0,希望一些新鲜的眼睛可以找到错误

<?php
function GoogleSerp($searchquery, $searchurl){
if(!empty($searchquery) && !empty($searchurl))
{
    $query = str_replace(" ","+",$searchquery);
    $query = str_replace("%26","&",$query);

// How many results to search through.

    $total_to_search = 50;

// The number of hits per page.

    $hits_per_page = 10;

// Obviously, the total pages / queries we will be doing is
// $total_to_search / $hits_per_page

// This will be our rank

    $position = 0;

// This is the rank minus the duplicates

    $real_position = 0;

    $found = NULL;
    $lastURL = NULL;

    for($i=0;$i<$total_to_search && empty($found);$i+=$hits_per_page)
    {

// Open the search page.
// We are filling in certain variables -
// $query,$hits_per_page and $start.

      //  $filename = "http://www.google.co.uk/xhtml?q=$query&start=$i&sa=N";
        $filename = "http://www.google.co.uk/m?q=$query&num=$hits_per_page&filter=0&start=$i&sa=N";

        $file = fopen($filename, "r");
        if (!$file)
        {
            return "error";
        }
        else
        {

// Now load the file into a variable line at a time

            while (!feof($file))
            {
                $var = fgets($file, 1024);

// Try and find the font tag google uses to show the site URL

                if(eregi("<span class=\"c\">(.*)</span>",$var,$out))
                {

// If we find it take out any <B> </B> tags - google does
// highlight search terms within URLS

                    $out[1] = strtolower(strip_tags($out[1]));

// Get the domain name by looking for the first /

                    $x = strpos($out[1],"/");

// and get the URL

                    $url = substr($out[1],0,$x);
                    $url = str_replace("/","",$url);
                    $position++;

// If you want to see the hits, set $trace to something

                 //   if($trace)return($url."<br>");

// If the last result process is the same as this one, it
// is a nest or internal domain result, so don't count it
// on $real_position

                    if(strcmp($lastURL,$url)<>0)$real_position++;

                    $lastURL = $url;

// Else if the sites match we have found it!!!

                    if(strcmp($searchurl,$url)==0)
                    {
                        $found = $position;

// We quit out, we don't need to go any further.

                        break;
                    }
                }
            }
        }
        fclose($file);
    }

    if($found)
    {
       $result = $real_position;
    }else{
        $result = 0;
    }
}
return $result;
}
?>

1 个答案:

答案 0 :(得分:0)

尝试urlencode()而不是查询中的两个替换。