通过谷歌搜索API获取超过10个结果

时间:2014-05-13 08:44:27

标签: php

我试图使用下面的鳕鱼列出10页的结果。当我直接运行URL时,我得到一个json字符串,但在代码中使用它不会返回任何内容。请告诉我我在哪里做错了。

$url =  "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=CompTIA A+ Complete Study Guide Authorized Courseware site:.edu&start=20";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$body = curl_exec($ch);
curl_close($ch);

$json = json_decode($body,true);
    print_r($json);

现在我使用以下代码,但它只输出一个页面的四个条目。请告诉我我在哪里做错了。

$term = "CompTIA A+ Training Kit Microsoft Press Training Kit";
for($i=0;$i<=90;$i+=10)
        {
            $term = $val.' site:.edu';
            $query = urlencode($term);
            $url   = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=' . $query . '&start='.$i;

            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
            $body = curl_exec($ch);
            curl_close($ch);

            $json = json_decode($body,true);

            //print_r($json);
            foreach($json['responseData']['results'] as $data)
            {
                echo '<tr><td>'.$i.'</td><td>'.$url.'</td><td>'.$k.'</td><td>'.$val.'</td><td>'.$data['visibleUrl'].'</td><td>'.$data['unescapedUrl'].'</td><td>'.$data['content'].'</td></tr>';
            }
        }

1 个答案:

答案 0 :(得分:0)

试试urlencode

$query = urlencode('CompTIA A+ Complete Study Guide Authorized Courseware site:.edu');
$url   = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=' . $query . '&start=20';