Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/246.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/typescript/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 始终返回0,Google SERP检查器_Php_Html - Fatal编程技术网

Php 始终返回0,Google SERP检查器

Php 始终返回0,Google SERP检查器,php,html,Php,Html,我的函数在google上搜索特定的关键字,然后检查站点,然后返回它在google上的位置(它用于我的seo仪表板),但它总是返回0,希望一些新的眼睛可以找到错误 <?php function GoogleSerp($searchquery, $searchurl){ if(!empty($searchquery) && !empty($searchurl)) { $query = str_replace(" ","+",$searchquery); $que

我的函数在google上搜索特定的关键字,然后检查站点,然后返回它在google上的位置(它用于我的seo仪表板),但它总是返回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;
}
?>


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

我注意到谷歌有时会阻止来自脚本的请求。他们没有为此提供API吗?对不起,你能给我一个例子吗?使用
$query=urlencode($searchquery),而不是两个str_replace()Allrite。我用原始参数尝试了您的脚本(正如我在原始源代码中看到的),它工作正常(得到“2”)。问题的原因很简单:你的网站不在名单上。注意,某些客户端会根据您的浏览器或语言生成特殊的结果列表。如果您想要完全相同的结果,可以伪造浏览器标识,但使用
fopen()
是不可能的。使用
cURL
:但我知道事实上我是第一页的。只需用GoogleSerp(“microsoft”,“www.microsoft.com”);-我得到“1”。