php - How to loop when using a Google api

44

The following code returns 10 results from a google api account I have set up, this is the max google will allow at one time, but you loop it 10 times. I wan't to get 100 results

<?php
{
$rootUri = "https://www.googleapis.com/customsear-----";        
$ApiKey = "

I can change where to start from at $start = , so basically I want to display 100 results, Thanks in advance

Here is the output

{-code-2}
975

Answer

---"; $CustomKey = "
146

Answer

------"; $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); ?>|||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. ) )
71

Answer

Solution:

Just a try but maybe it works like this:

$rootUri = "https://www.googleapis.com/customsear-----";        
$ApiKey = "
511

Answer

---"; $CustomKey = "
742

Answer

------"; $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);

People are also looking for solutions to the problem: php - Magento Navigation Menu Bar With Thumbnail image

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.