-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecarta.php
More file actions
executable file
·103 lines (81 loc) · 1.93 KB
/
Decarta.php
File metadata and controls
executable file
·103 lines (81 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
<?php
/**
* Decarta API class (http://api.decarta.com/)
*
* @author Keith Palmer <keith@consolibyte.com>
*/
class Decarta
{
protected $_api_key;
const TYPE_GEOCODE = 'geocode';
const TYPE_SEARCH = 'search';
const ERROR_OK = 200;
const ERROR_UNAUTHORIZED = 401;
const ERROR_SERVER = 500;
const ERROR_TEAPOT = 418;
const ENDPOINT = 'http://api.decarta.com/v1/[KEY]/[TYPE]/[SEARCH].json';
public function __construct($api_key)
{
$this->_api_key = $api_key;
}
protected function _getEndpoint($type, $str, $params = array())
{
$url = str_replace(
array(
'[TYPE]',
'[KEY]',
'[SEARCH]'
),
array(
$type,
$this->_api_key,
urlencode($str),
),
Decarta::ENDPOINT);
if (is_array($params) and
count($params))
{
$url .= '?' . http_build_query($params);
}
return $url;
}
public function lastRequest()
{
return $this->_last_request;
}
public function lastResponse()
{
return $this->_last_response;
}
public function lastError()
{
return $this->_last_error;
}
public function search($str, $params = array())
{
return $this->_request(Decarta::TYPE_SEARCH, $str, $params);
}
public function geocode($str, $params = array())
{
return $this->_request(Decarta::TYPE_GEOCODE, $str, $params);
}
protected function _request($type, $str, $params)
{
$url = $this->_getEndpoint($type, $str, $params);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$this->_last_request = $url;
$retr = curl_exec($ch);
$info = curl_getinfo($ch);
$this->_last_response = print_r($info, true) . "\r\n\r\n" . $retr;
curl_close($ch);
if ($info['http_code'] == Decarta::ERROR_OK)
{
$this->_last_error = Decarta::ERROR_OK;
$obj = json_decode($retr);
return $obj->results;
}
$this->_last_error = $info['http_code'];
return false;
}
}