سؤال

I am trying to get more than 10 results form google using the search API. I know that the google search API only gives 10 results and you have to call it 10 times to get a hundred but I can't seem to get it working. I tried creating a do while loop as well as a for loop but all it seems to do is gives me the same results over and over.

<?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);


}
?>

Any input appreciated.

هل كانت مفيدة؟

المحلول

ok. just try the code below:

<?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>';
        }
    }

}
?>
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top