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.

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

报告相同问题?

悬赏问题

  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛
  • ¥30 python代码,帮调试,帮帮忙吧