dongqing5925 2012-10-07 19:01
浏览 47
已采纳

使用Google API进行域索引检查

I am trying to build website where a user can enter his website and check his indexed pages in google. Just like we do by site:domain.com

I am using a google API, here is the link to API (I know it's deprecated)

http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=

when I use a code like

<?php  

function getGoogleCount($domain) {
    $content = file_get_contents('http://ajax.googleapis.com/ajax/services/' .
        'search/web?v=1.0&filter=0&q=site:' . urlencode($domain));
    $data = json_decode($content);
    return ($data->responseData->cursor->estimatedResultCount);
}

echo getGoogleCount('stackoverflow.com');

?>

good as far as I want to know about the result counts But the next thing I want is to list all the results on my website. I am unable to grab the results because when we write

$data->responseData->cursor->estimatedResultCount

It goes straight But when we try to get results. I don't know how to do, just to print the idea

$data->responseData->results->[url & title & content here]

Because here results is an array. and I don't know how can I store info in an array in this case.

Have been searching for a long time but can't find anything related.....

Thanks in advance...

  • 写回答

1条回答 默认 最新

  • dongwuxie7976 2012-10-07 20:15
    关注

    The easiest way is to use:

    $data = json_decode($content, true);
    

    That will convert objects to normal associative arrays which are probably easier to handle. Then you access your values by something like this:

    $results = $data['responseData']['results']; //array
    $googleCount = $data['responseData']['cursor']['estimatedResultCount'];
    

    Then with the results you can do something like this:

    foreach ($results as $result) {
        echo $result['title'].' -> '.$result['url'] . '<br />';
    }
    

    But of course if you don't like associative arrays and you prefer objects, you could do it also this way:

    $data = json_decode($content);
    foreach ($data->responseData->results as $result) {
        echo $result->title .' -> '.$result->url.'<br />';
    }
    

    If you want to check which properties has the $result, just use print_r or var_dump.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥85 maple软件,solve求反函数,出现rootof怎么办?
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥15 maple软件,用solve求反函数出现rootof,怎么办?
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题
  • ¥20 求一个html代码,有偿
  • ¥100 关于使用MATLAB中copularnd函数的问题
  • ¥20 在虚拟机的pycharm上