-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgoogle.php
171 lines (160 loc) · 3.64 KB
/
google.php
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
<?php
/**
Google plugin for the PHP Fat-Free Framework
The contents of this file are subject to the terms of the GNU General
Public License Version 3.0. You may not use this file except in
compliance with the license. Any of the license terms and conditions
can be waived if you get permission from the copyright holder.
Copyright (c) 2009-2012 F3::Factory
Bong Cosca <[email protected]>
@package Google
@version 2.0.12
**/
//! Collection of Google API adaptors
class Google extends Base {
//@{ Locale-specific error/exception messages
const
TEXT_Image='Unsupported image format';
//@}
/**
Call Geocoding API and return geographical coordinates of
specified address
@return array
@param $addr string
**/
static function geocode($addr) {
$result=json_decode(
Web::http(
'GET http://maps.googleapis.com/maps/api/geocode/json?',
http_build_query(
array(
'address'=>$addr,
'sensor'=>'false'
)
)
),
TRUE
);
if (!isset($result['results'])) {
trigger_error($result['status']);
return FALSE;
}
return $result['results'][0]['geometry'];
}
/**
Generate static map using Google Maps API
@param $center string
@param $zoom integer
@param $size string
@param $type string
@param $format string
@param $language string
@param $markers array
@param $die bool
@public
**/
static function
staticmap(
$center,
$zoom=15,
$size='400x400',
$type='roadmap',
$format='png',
$language='en',
array $markers=NULL,
$die=TRUE) {
preg_match('/(gif|jp[e]*g|png)$/i',$format,$ext);
if ($ext) {
$ext[1]=str_replace('jpg','jpeg',strtolower($ext[1]));
if (PHP_SAPI!='cli' && !headers_sent())
header(self::HTTP_Content.': image/'.$ext[1]);
echo Web::http(
'GET http://maps.google.com/maps/api/staticmap',
http_build_query(
array_merge(
array(
'center'=>$center,
'zoom'=>$zoom,
'size'=>$size,
'maptype'=>$type,
'format'=>$format,
'language'=>$language,
'sensor'=>'true'
),
$markers?$markers:array()
)
)
);
}
else
trigger_error(self::TEXT_Image);
if ($die)
die;
}
/**
Web search using Google AJAX Search API
@return mixed
@param $text string
@param $page integer
@public
**/
static function search($text,$page=0) {
$result=json_decode(
Web::http(
'GET http://ajax.googleapis.com/ajax/services/search/web',
http_build_query(
array(
'v'=>'1.0',
'q'=>$text,
'rsz'=>'large',
'start'=>8*$page
)
)
),
TRUE
);
if (is_null($result['responseData'])) {
trigger_error($result['responseDetails']);
return FALSE;
}
foreach ($result['responseData']['results'] as &$data)
$data=array(
'url'=>$data['unescapedUrl'],
'title'=>$data['title'],
'content'=>$data['content']
);
return array(
'page'=>$result['responseData']['cursor']['currentPageIndex'],
'results'=>$result['responseData']['results']
);
}
/**
Retrieve Atom/RSS feed using Google AJAX Feed API; If second
argument is TRUE, XML string returned; Otherwise, a PHP array
@return mixed
@param $url string
@param $isxml boolean
@public
**/
static function feed($url,$isxml=TRUE) {
$result=json_decode(
Web::http(
'GET http://ajax.googleapis.com/ajax/services/feed/load',
http_build_query(
array(
'v'=>'1.0',
'q'=>$url,
'num'=>'-1',
'output'=>$isxml?'xml':'json'
)
)
),
TRUE
);
if (is_null($result['responseData'])) {
trigger_error($result['responseDetails']);
return FALSE;
}
return $result['responseData'][$isxml?'xmlString':'feed'];
}
}