dragon5006 2019-04-03 14:21
浏览 35

在PHP脚本中检查Google搜索结果的数量

Is it possible to check number of Google search results of a certain phrase in a PHP script with Google API? I am curious if it can be done someway. If yes, what should I do?

EDIT

Okay, according to drakin8564 advices it is my function:

function google(String $text) {

        // create curl resource 
        $ch = curl_init(); 

        // set url 
        curl_setopt($ch, CURLOPT_URL, 'https://www.google.com/search?q="'.$text.'"'); 

        //return the transfer as a string 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 

        libxml_disable_entity_loader(false);
        // $output contains the output string 
        $output = curl_exec($ch); 

        $xml = new DOMDocument();

        $xml->loadHTMLFile($output);

        $string = $xml->getElementById('resultStats');

        $string = str_replace('Około ', '', $string); 
        $string = str_replace(',', '', $string);
        $results = intval($string);

        if($results>0) {

            curl_close($ch);            
            return true; 
        } else {
        curl_close($ch);     
        return false;
        // close curl resource to free up system resources 
        } 
        } 

NOTE: "Około" means "About" in Polish, Also I have a warning:

Warning: DOMDocument::loadHTMLFile(): I/O warning : failed to load external entity "<!doctype html><html itemscope="" itemtype="http://schema.org/SearchResultsPage" lang="pl"><head><meta content="text/ in C:\xampp2\htdocs\file.php on line 19

And here is an implementation

if(google("flower") == true){
            echo "search results exist";

        } else {
            echo "results don't exist";
        }

if(google("7dsa8dsaza") == true){
            echo "search results exist";

        } else {
            echo "results don't exist";
        }

Unfortunately there is a problem because both examples cause "search results exist" output while the results for "7dsa8dsaza" do not exist. How to fix it?

  • 写回答

1条回答 默认 最新

  • dpfw3607 2019-04-03 14:34
    关注

    I don't know of a way to do it with Google API, but you could curl to https://www.google.com/search?q={your+search+words}

    You should be able to throw that html into an object using DomDocument. An estimated number of results is in a div with id="resultStats"

    Examples of using the php curl functions: PHP cURL Docs

    Docs for DomDocument, has some examples in the comments section: PHP DomDocument Docs

    EDIT: Example to get only the number from the resultStats element:

    $string = "About 214,000,000 results (0.41 seconds)"; 
    $string = str_replace('About ', '', $string); 
    $string = str_replace(',', '', $string);
    $results = intval($string);
    
    评论

报告相同问题?

悬赏问题

  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类
  • ¥15 微带串馈天线阵列每个阵元宽度计算
  • ¥15 keil的map文件中Image component sizes各项意思