开发者

Always returning 0, Google SERP Checker

My function searches google for the specific keyword and then checks for the 开发者_JAVA百科site and then returns the what position it is on google (its for my seo dashboard) but it always return's 0, hopefully some fresh eyes can find the faults

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


Try urlencode() instead of the two replaces on the query.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜