Curl PHP Google Custom Search

0

I have this script to do a google search using Google Custom Search . My problem is that when you do the form request returns blank, what would be the problem?

<?php


define('GOOGLE_API_KEY', 'AIzaSyBLx8EBzpqtwDXZKobQCBpBR893ABlefZc');

function curl_get($url, $params)
{
    $post_params = array();
    foreach ($params as $key => &$val) {
      if (is_array($val)) $val = implode(',', $val);
        $post_params[] = $key.'='.urlencode($val);
    }
    $post_string = implode('&', $post_params);

    $fullurl = $url."?".$post_string;

    $ch = curl_init();
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_URL, $fullurl);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mailana (curl)');
    $result = curl_exec($ch);
    curl_close($ch);

    return $result;
}


function perform_google_web_search($termstring)
{
    $start = 0;
    $result = array();
    while ($start<50)



    {
        $searchurl = 'https://www.googleapis.com/customsearch/v1?';
        $searchurl .= 'key='.GOOGLE_API_KEY;
        $searchurl .= '&cx=017576662512468239146:omuauf_lfve';
        $searchurl .= '&q='.urlencode($termstring);
        $response = curl_get($searchurl, array());

        $responseobject = json_decode($response, true);

        if (count($responseobject['responseData']['results'])==0)
            break;

        $allresponseresults = $responseobject['responseData']['results'];

        foreach ($allresponseresults as $responseresult)
        {
            $result[] = array(
                'url' => $responseresult['url'],
                'title' => $responseresult['title'],
                'abstract' => $responseresult['content'],
            );
        }

        $start += 8;
    }

    return $result; 
}

if (isset($_REQUEST['q'])) {
    $termstring = urldecode($_REQUEST['q']);
} else {
    $termstring = '';
}

?>
<html>
<head>
<title>Google</title>
</head>
<body>
<div style="padding:20px;">
<center>
<form method="GET" action="">
Search terms: <input type="text" size="40" name="q" value='<?=$termstring?>'/>
</form>
</center>
</div>
<?php


if ($termstring!='') {

    $googleresults = perform_google_web_search($termstring);


    print '<br/><br/><h2>Google search results</h2><br/>';
    foreach ($googleresults as $result) {
        print '<a href="'.$result['url'].'">'.$result['title'].'</a><br/>';
        print '<span style="font-size:80%">'.$result['abstract'].'</span><br/><hr/>';
    }

}

?>
    
asked by anonymous 23.02.2018 / 12:48

1 answer

0

try changing

if (isset($_REQUEST['q'])) {
    $termstring = urldecode($_REQUEST['q']);
} else {
    $termstring = '';
}

by

if (isset($_GET['q'])) {
    $termstring = urldecode($_GET['q']);
} else {
    $termstring = '';
}
    
23.02.2018 / 13:28