2013-07-21 68 views
-1

我想要使用搜索API獲取超過10個結果表單。我知道谷歌搜索API只給出10個結果,你必須調用它10次才能得到一百個,但我似乎無法得到它的工作。我試着創建一個do while循環以及for循環,但它似乎所做的一切都給了我一遍又一遍的相同結果。獲得超過10個結果Google搜索API

<?php 


if(isset($_GET['input']) && $_GET['input'] != "") 
{ 

    echo "<br />Your Search Results Google:<br /><br />"; 


    $i=0; 



    $url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0& key=AIzaSyBacVRiPNo7uMqhtjXG4Zeq1DtSQA_UOD4&cx=014517126046550339258:qoem7fagpyk 
&num=10&start=".$i."&"."q=".str_replace(' ', '%20', $_GET['input']) 

// sendRequest 
// note how referer is set manually 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_REFERER, 'http://www.google.com'); 
$body = curl_exec($ch); 
    curl_close($ch); 





// now, process the JSON string 




$json = json_decode($body,true); 


do 
{ 

foreach ($json['responseData']['results'] as $data) { 
echo ' 
    <p> 
     ', $data ['title']," ---> <u>Google SE </u>" ,'<br /> 
     ', '<a href ='.$data['url'].'>'.$data['url']."</a>" , '<br /> 
     ', $data['content'],' 
    </p>'; 
} 

$i++;  

} 
while($i<3); 


} 
?> 

理解的任何輸入。

+1

您的循環出現問題。 – srain

回答

2

好的。只需嘗試以下代碼:

<?php 
if(isset($_GET['input']) && $_GET['input'] != "") 
{ 
    echo "<br />Your Search Results Google:<br /><br />"; 

    for ($i = 0; $i < 10; $i++) 
    { 

     $url = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&key=AIzaSyBacVRiPNo7uMqhtjXG4Zeq1DtSQA_UOD4&cx=014517126046550339258:qoem7fagpyk 
      &num=10&start=".$i."&"."q=".str_replace(' ', '%20', $_GET['input']); 
     $ch = curl_init(); 
     curl_setopt($ch, CURLOPT_URL, $url); 
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
     curl_setopt($ch, CURLOPT_REFERER, 'http://www.google.com'); 
     $body = curl_exec($ch); 
     curl_close($ch); 

     $json = json_decode($body,true); 

     foreach ($json['responseData']['results'] as $data) { 
      echo ' 
       <p> 
       ', $data ['title']," ---> <u>Google SE </u>" ,'<br /> 
       ', '<a href ='.$data['url'].'>'.$data['url']."</a>" , '<br /> 
       ', $data['content'],' 
       </p>'; 
     } 
    } 

} 
?> 
+0

不等它沒有輸出相同的結果100次。回到畫板 –

+0

什麼是您的搜索字詞?讓我試試看。 – srain

+0

我已經刪除&key = AIzaSyBacVRiPNo7uMqhtjXG4Zeq1DtSQA_UOD4&cx = 014517126046550339258:qoem7fagpyk這部分,它對我來說太棒了! – Ashish

相關問題