Curl PHP Google Custom Search

Asked

Viewed 214 times

0

I have this script to do a google search using the Google Custom Search. My problem is that when you request the form 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/>';
    }

}

?>
  • Which version of php ?

  • the version of my PHP is 5

  • am sweating php 7 and resume a Count() error: Parameter must be an array or an Object that Implements Countable in C: xampp htdocs teste.php on line 47

  • 1

    I removed my answer because it was misinterpretation of the question, what returns blank is the result and not the field input form. I will read the documentation of this API and test tonight and see if I resolve to post a new response.

1 answer

0

try to change

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

for

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.