0

以下代码从我设置的 google api 帐户返回 10 个结果,这是 google 一次允许的最大值,但你循环它 10 次。我不想得到 100 个结果

<?php
{
$rootUri = "https://www.googleapis.com/customsear-----";        
$ApiKey = "-------------";
$CustomKey = "----------------";
$query = urlencode("'{$_POST['query']}'");

$start = "1";  // the next loop starts at 11, then 21 until 91

    $link=$rootUri."key=".$ApiKey."&cx=".$CustomKey."&q=".$query."&alt=json"."&start=".$start;

    $ch=curl_init();
    curl_setopt($ch,CURLOPT_URL,$link);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    $data = curl_exec($ch);
    curl_close($ch);

    $js = json_decode($data); 

    $googleArray = array();                     

    foreach ($js->items as $item)
    {   
        $googleArray[$i]['link'] = ($item->{'link'});           
        $googleArray[$i]['title'] = ($item->{'title'});
        $googleArray[$i]['snippet'] = ($item->{'snippet'});
        $i++;
    }
    print_r ($googleArray);

?>

我可以更改从 $start = 开始的位置,所以基本上我想显示 100 个结果,提前致谢

这是输出

Array ( [1] => Array ( [link] => https://www.facebook.com/ [title] => Welcome to Facebook - Log In, Sign Up or Learn More [snippet] => Facebook is a social utility that connects people with friends and others who work , study and live around them. People use Facebook to keep up with friends, ... ) [2] => Array ( [link] => http://www.facebook.com/home.php [title] => Facebook [snippet] => Facebook is a social utility that connects people with friends and others who work , study and live around them. People use Facebook to keep up with friends, ... ) [3] => Array ( [link] => https://m.facebook.com/ [title] => Welcome to Facebook [snippet] => Facebook helps you connect and share with the people in your life. ) [4] => Array ( [link] => https://www.facebook.com/login.php [title] => Facebook Login [snippet] => Facebook is a social utility that connects people with friends and others who work , study and live around them. People use Facebook to keep up with friends, ... ) [5] => Array ( [link] => http://en.wikipedia.org/wiki/Facebook [title] => Facebook - Wikipedia, the free encyclopedia [snippet] => Facebook is an online social networking service, whose name stems from the colloquial name for the book given to students at the start of the academic year by  ... ) [6] => Array ( [link] => https://itunes.apple.com/us/app/facebook/id284882215?mt=8 [title] => Facebook for iPhone, iPod touch, and iPad on the iTunes App Store [snippet] => Jul 10, 2013 ... Facebook is only available for users age 13 and over. Our Data Use Policy and EULA are available in the legal section of our App Store ... ) [7] => Array ( [link] => http://topics.nytimes.com/top/news/business/companies/facebook_inc/index.html [title] => Facebook Inc. News - Company Information - The New York Times [snippet] => Facebook Inc. financial and business news, updates, and information from The New York Times and other leading providers. ) [8] => Array ( [link] => https://play.google.com/store/apps/details?id=com.facebook.katana&hl=en [title] => Facebook - Android Apps on Google Play [snippet] => Keeping up with friends is faster than ever. • See what friends are up to • Share updates, photos and videos • Get notified when friends like and comment on yo... ) [9] => Array ( [link] => http://mashable.com/category/facebook/ [title] => Facebook [snippet] => Facebook is the world's largest social network, with more than 900 million users. Facebook was founded in 2004 by Mark Zuckerberg while he was an ... ) [10] => Array ( [link] => https://bg-bg.facebook.com/home [title] => Facebook Home | Facebook [snippet] => Introducing Facebook Home, a family of apps that puts your friends at the heart of your phone. ) )
4

1 回答 1

0

只是一个尝试,但也许它是这样工作的:

$rootUri = "https://www.googleapis.com/customsear-----";        
$ApiKey = "-------------";
$CustomKey = "----------------";
$query = urlencode("'{$_POST['query']}'");


$all_items = array();
for($i=1;$i<100;$i+=10) {

    $start = $i; 
    $link=$rootUri."key=".$ApiKey."&cx=".$CustomKey."&q=".$query."&alt=json"."&start=".$start;

    $ch=curl_init();
    curl_setopt($ch,CURLOPT_URL,$link);
    curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
    $data = curl_exec($ch);
    curl_close($ch);
    $js = json_decode($data); 
    $all_items = array_merge($all_items,$js->items);
}


$googleArray = array();                     

foreach ($all_items as $item)
{   
    $googleArray[$i]['link'] = ($item->{'link'});           
    $googleArray[$i]['title'] = ($item->{'title'});
    $googleArray[$i]['snippet'] = ($item->{'snippet'});
    $i++;
}
print_r ($googleArray);
于 2013-07-26T12:11:47.160 回答