I'm working on my project of using Google CSE. At this step I'm writing codes to retrieve JSON results from it. Here's the code:
<?php
function cURL($url, $ref, $p) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_REFERER, $ref);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
if ($p) {
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $p);
}
$result = curl_exec($ch);
curl_close($ch);
if ($result) {
return $result;
} else {
return '';
}
}
if (isset($_GET['keyword'])) {
$keyword = $_GET['keyword'];
} else {
echo 'salah bro!';
}
$cseNumber = 'aaa'; // Key for the API thing
$key = 'bbb'; //Key for Nofriani's account: sorta a password
$start = '1';
$file = cURL('https://www.googleapis.com/customsearch/v1?key=' . $key . '&cx=' . $cseNumber . '&q=' . $keyword . '&siteSearchFilter=i&alt=json&start=' . $start, 'https://www.googleapis.com/customsearch/v1?key=' . $key . '&cx=' . $cseNumber . '&q=' . $keyword . '&siteSearchFilter=i&alt=json&start=' . $start, null);
echo $file;
?>
It worked perfectly and I got 10 first results of Google CSE. Unfortunately, the API restricts that only 10 results can be retrieved in bulk. Now, I intend to take 100 results in one results page (not in separated 10 page), by doing a looping. I added this:
$start= '';
$file = cURL('https://www.googleapis.com/customsearch/v1?key=' . $key . '&cx=' . $cseNumber . '&q=' . $keyword . '&siteSearchFilter=i&alt=json&start=' . $start, 'https://www.googleapis.com/customsearch/v1?key=' . $key . '&cx=' . $cseNumber . '&q=' . $keyword . '&siteSearchFilter=i&alt=json&start=' . $start, null, $start);
return $file;
function getResult() {
for ($i = 0; $i <= 90; $i+=10) {
$file .= cURL($url, $ref, $p, ($i + 1));
for ($j = 0; $j < 10; $j++) {
echo $file;
}
}
}
?>
But it didn't work just like it did. I tried some tips here: PHP How can I open several sources using curl? But it didn't work as well, :( Can anyone please help me solve this? Thanks..
You forgot so many things like parameter and concatenation. Proper function will be like this
Update
function getResult($key,$cseNumber,$keyword) { // parameter added
for ($i = 1; $i <= 10; $i++) {
$file = cURL('https://www.googleapis.com/customsearch/v1?key=' . $key . '&cx=' . $cseNumber . '&q=' . $keyword . '&siteSearchFilter=i&alt=json&start=' . $i, 'https://www.googleapis.com/customsearch/v1?key=' . $key . '&cx=' . $cseNumber . '&q=' . $keyword . '&siteSearchFilter=i&alt=json&start=' . $i, null); // . removed from here
echo $file;
}
}