PHP Classes

File: example.php

Recommend this page to a friend!
  Classes of Dave Smith   serpStack PHP Google Search API   example.php   Download  
File: example.php
Role: Example script
Content type: text/plain
Description: Example Usage
Class: serpStack PHP Google Search API
Perform searches on Google using the serpStack API
Author: By
Last change:
Date: 4 years ago
Size: 1,177 bytes
 

Contents

Class file image Download
<?php
/*
example usage
serpStack ver 1.0

You must get an API key from https://weatherstack.com/product
and enter it in the weatherstack.class.php file
*/

//turning off low level notices
error_reporting(E_ALL ^ E_NOTICE);

//instantiate the class
include('serpstack.class.php');
$serp = new serpStack();

//set the end point to use
//serp scraping uses the 'search' endpoint
//we can set it with 'search' or 's' shorthand
$serp->setEndPoint('search');

//set the query parameter
$serp->setParam('query','mcdonalds');

//optionally set to a safe search
$serp->setParam('safe','1');

//get the response
$serp->getResponse();

//check for errors
if( !empty($serp->errorCode) ){
   
//error returned
   
echo $serp->errorCode.': '.$serp->errorText;
}else{
   
//loop through and show specific response data
   
foreach($serp->response->organic_results as $result){
   
?>
<h5><a href="<?php echo $result->url;?>"><?php echo $result->title;?></a></h5>
        <div><?php echo $result->snippet;?></div>
        <hr>
    <?php
   
}
}

//displaying the full response
echo '<pre>';
var_dump($serp->response);
echo
'<pre>';
?>