2013-07-26 45 views
0

以下代碼返回10個谷歌api帳​​戶的結果我已經設置,這是最大的谷歌將允許在一次,但你循環它10次。我wan't得到100個結果如何使用谷歌api時循環

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

?> 

我可以改變從何處開始在$啓動=,所以基本上我想顯示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.)) 
+0

它不工作或者你想知道你可以重複它10次? – steven

+0

它確實有效,但我希望它重複10次,但是從第二次開始11,第三次等等... – user2622398

回答

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); 
+0

在「curl_setopt($ ch,CURLOPT_URL,$ link)」獲得了此警告「未定義變量:鏈接」 ;」並且此警告「試圖獲取非對象的屬性」在for循環 – user2622398

+0

結束我的第一篇文章是不正確的。發佈後1分鐘我做了一個修改。你有沒有試過當前的代碼? – steven