Skip to content

Instantly share code, notes, and snippets.

@kaugesaar
Created September 17, 2014 20:51
Show Gist options
  • Save kaugesaar/ec518152ec38720e3f06 to your computer and use it in GitHub Desktop.
Save kaugesaar/ec518152ec38720e3f06 to your computer and use it in GitHub Desktop.
<?php
class GeoSearch {
/**
* Base URL for Google
*
* @var string
*/
public $baseUrl = 'https://www.google.se/search?';
/**
* Append pws parameter to url
*
* @var bool
*/
public $pws = true;
/**
* Append lang parameter to url
*
* @var string
*/
public $lang;
/**
* Initialize GeoSearch
*/
public function __construct()
{
$this->key = array_merge(range('A','Z'), range('a','z'), range('0','9'), array('-', '_'));
}
/**
* Returns the hash Google uses for &uule=
*
* @param $location
* @return string
*/
public function makeHash($location)
{
return trim('w+CAIQICI'.$this->key[strlen($location)%count($this->key)].base64_encode($location),'=');
}
/**
* Returns the full search URL
*
* @param array $input
* @return string
*/
public function build($input = [])
{
$hash = $this->makeHash($input['location']);
$params = [
'q' => $input['query'],
'uule' => ''
];
if($this->pws) $params = array_merge(['pws' => '0'],$params);
if($this->lang) $params = array_merge(['hl' => $this->lang],$params);
$url = $this->baseUrl.http_build_query($params).$hash;
return $url;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment