-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathquery-ga-name.php
85 lines (58 loc) · 2.13 KB
/
query-ga-name.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
<?php
// use curl to query google contacts
// to test in browser: http://example.com/query-ga-name.php?q=8675309
// to use, include() this file in your own php and call: queryGoogle("8675309");
// potatoes
if ( isset($_GET['q']) ) {
// for testing & troubleshooting
echo "<pre>\n";
// print_r(queryGoogle($_GET['q']));
var_dump(queryGoogle($_GET['q']));
}
exit;
// meat
function queryGoogle ($q) {
// query should be numbers or letters only, no brackets, dashes, spaces or other.
// google will return multiple results if they exist
if ( !isset($q) ) { $names[error]='no query'; return $names;}
$names[q] = preg_replace('/[^A-Za-z0-9]/', '', $q);
// retreive access token
$tokenData = include 'refresh-ga-token.php';
if ($tokenData==null) { $names[error]='token failed'; return $names;}
$url="https://www.google.com/m8/feeds/contacts/default/full?alt=json&q=" . $names[q];
$header = array (
"Gdata-version: 3.0",
"Authorization: $tokenData[token_type] $tokenData[access_token]"
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPGET, 1); // get, not post
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); // return to string instead of printing it
$result = curl_exec($ch);
$result = json_decode($result,true);
$info = curl_getinfo($ch);
curl_close($ch);
// Check HTTP status code
if ($info['http_code'] === 200) {
// $t is NOT a variable, it's actually an array key name returned by google. WTF?
$names[account] = $result['feed']['id']['$t'];
$names[total_results] = $result['feed']['openSearch$totalResults']['$t'];
$c="";
for ($x = 0; $x < $names[total_results]; $x++) {
// We add * to all our work contact names to differeniate them from personal contacts.
// Sometimes results are returned with a comma on the end of the name.
// Use rtrim to clean things up.
$n = $result['feed']['entry'][$x]['gd$name']['gd$fullName']['$t'];
$n = rtrim($n, '*');
$n = rtrim($n, ' ');
$n = rtrim($n, ',');
$names['list'] .= $c . $n;
$c=", ";
}
} else {
$names[error]='curl failed';
}
return $names;
}
?>