PHP code example of serpwow / google-search-results

1. Go to this page and download the library: Download serpwow/google-search-results library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

serpwow / google-search-results example snippets




// composer autoload
ct, passing in our API key
$serpwow = new GoogleSearchResults("API_KEY");

// set up the search parameters
$params = [
  "q" => "pizza"
];

// retrieve the search results as JSON
$result = $serpwow->json($params);

// pretty-print the JSON result
print_r($result);




// composer autoload
ct, passing in our API key
$serpwow = new GoogleSearchResults("API_KEY");

// set up the search parameters
$params = [
  "q" => "pizza",
  "location" => "New York,New York,United States"
];

// retrieve the search results as JSON
$result = $serpwow->json($params);

// pretty-print the JSON result
print_r($result);




// composer autoload
ct, passing in our API key
$serpwow = new GoogleSearchResults("API_KEY");


// perform a search on Google News, just looking at blogs, ordered by date, in the last year, filtering out duplicates
$params = [
  "q" => "football news",
  "search_type" => "news",
  "news_type" => "blogs",
  "show_duplicates" => "false",
  "sort_by" => "date",
  "time_period" => "last_year"
];
// retrieve the search results as JSON
$result = $serpwow->json($params);
// pretty-print the JSON result
print_r($result);


// perform a search on Google Places for 'plumber' in London
$params = [
  "q" => "football news",
  "search_type" => "places",
  "location" => "London,England,United Kingdom"
];
$result = $serpwow->json($params);
print_r($result);


// perform an image search on Google Images for "red flowers"
$params = [
  "q" => "red flowers",
  "search_type" => "images"
];
$result = $serpwow->json($params);
print_r($result);




// composer autoload
ct, passing in our API key
$serpwow = new GoogleSearchResults("API_KEY");

/* 
  Set up parameters the query (q) and location parameters
  note that the "location" parameter should be a value
  returned from the Locations API.
  We'll re-use the same params for all 3 examples.
*/
$params = [
  "q" => "pizza",
  "location" => "New York,New York,United States"
];

// retrieve the search results as JSON
$result = $serpwow->json($params);
print_r($result);


// retrieve the Google search results as HTML
$result = $serpwow->html($params);
print_r($result);

// retrieve the Google search results as CSV
$result = $serpwow->csv($params);
print_r($result);




// composer autoload
ct, passing in our API key
$serpwow = new GoogleSearchResults("API_KEY");

// set up the mobile params
$paramsMobile = [
  "q" => "pizza",
  "device" => "mobile"
];

// set up the tablet params
$paramsTablet = [
  "q" => "pizza",
  "device" => "tablet"
];

// // set up the desktop params (note we omit the 'device' param)
$paramsDesktop = [
  "q" => "pizza"
];

// retrieve the mobile search results
$result = $serpwow->json($paramsMobile);
print_r($result);

// retrieve the tablet search results
$result = $serpwow->json($paramsTablet);
print_r($result);

// retrieve the desktop search results
$result = $serpwow->json($paramsDesktop);
print_r($result);




// composer autoload
ct, passing in our API key
$serpwow = new GoogleSearchResults("API_KEY");

// set up a simple query
$params = [
  "q" => "pizza"
];

// retrieve the search results as JSON
$result = $serpwow->json($params);

// determine if the request was successful
$success = $result->request_info->success;

if ($success) {
  
  // extract the time taken and number of organic results
  $timeTaken = $result->search_metadata->total_time_taken;
  $organicResultCount = sizeof($result->organic_results);

  // print
  print_r($organicResultCount.' organic results returned in '.$timeTaken.'s');
  
}




// composer autoload
/ create the serpwow object, passing in our API key
$serpwow = new GoogleSearchResults("API_KEY");

// set up a simple query
$params = [
  "q" => "pizza",
  "page" => 1,
  "num" => $numberOfResults
];

// retrieve the search results as JSON
$result = $serpwow->json($params);

// print out the number of organic results returned 
print_r(sizeof(result.organic_results).' organic results returned');




// composer autoload
ct, passing in our API key
$serpwow = new GoogleSearchResults("API_KEY");

// set up a simple query
$params = [
  "q" => "pizza",
  "gl" => "us",
  "hl" => "en",
  "location" => "New York,New York,United States",
  "google_domain" => "google.com",
  "time_period" => "custom",
  "sort_by" => "date",
  "time_period_min" => "02/01/2018",
  "time_period_max" => "02/08/2019",
  "device" => "mobile",
  "csv_fields" => "search.q,organic_results.position,organic_results.domain",
  "page" => "1",
  "num" => "100"
];

// retrieve the search results as CSV
$result = $serpwow->csv($params);

// print out the number of organic results returned 
print_r($result);




// composer autoload
ct, passing in our API key
$serpwow = new GoogleSearchResults("API_KEY");

// set up a simple locations query
$params = [
  "q" => "mumbai"
];

// retrieve locations matching the query parameters as JSON
$result = $serpwow->locations($params);

// print out the locations returned 
print_r($result);




// composer autoload
ct, passing in our API key
$serpwow = new GoogleSearchResults("API_KEY");

// get our account info
$result = $serpwow->account($params);

// print out the locations returned 
print_r($result);