I'm trying to get the first 50 results of the Google CSE API with the following PHP code.
The problem is that it combines the two pages sow the results are getting messed up like the first position is the one of the first page and the second position is the second of the second page and so on. Can someone tell me what I am doing wrong here?
What I actually want to do is get the first 50 results in a array, but the code below gives me mixed results.
$apiKey = "theapikey";
$query = "news";
for ($i = 1; $i <= 5; $i++) {
$ch = curl_init();
$request = "https://www.googleapis.com/customsearch/v1?q=" . urlencode( "$query" ) . "&cx=013594553343653397533:q-qkkaltmay" ."&key=" . $apiKey . "&start=" . $i;
curl_setopt($ch, CURLOPT_URL, $request);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
$output = curl_exec($ch);
$output = json_decode($output);
foreach( $output->items as $result ) {
$url = $result->link;
${"items" . $i}[] = $url;
}
}
echo json_encode($items1);