-
Notifications
You must be signed in to change notification settings - Fork 5
/
whodat.php
executable file
·93 lines (78 loc) · 2.96 KB
/
whodat.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
<?php
// enable JSONP - but remember, cookies have a domain scope too.
$callback = isset($_GET['callback']) ? $_GET['callback'] : false;
if ($callback) {
header('Content-type: text/javascript');
} else {
header('Content-type: text/plain');
}
$status = get_whodat_status();
// enclose in JSONP callback function if necessary
if ($callback) {
print "$callback(";
print $status;
print ");";
} else {
print $status;
}
/**
* Check to see if they user is valid Marketo lead.
* If they are, we add a permanant cookie so we don't need to hit the Marketo API again.
* If they are not we, add a session cookie.
* The cookies allow us to cache the API response.
*
* @param string $cookie
* @return whodat data (JSON)
*/
function get_whodat_status($cookie = 'whodat')
{
include 'settings.php'; // load in variables containing Marketo secret key, etc.
if (isset($_COOKIE[$cookie]))
{
// If the whodat cookie is already set, return its value.
// Ideally, the cookie would just be a pointer/ID reference to something like memcache, or a
// database. On our site we don't have that easily available, so for now just lazily store the
// JSON in a cookie.
// decode what's stored in the cookie and wrap it in some status JSON, returning
return json_encode(array(
"result" => true,
"from" => "cookie",
"data" => json_decode($_COOKIE[$cookie])
));
}
else {
// We access the Marketo API with the user's Marketo cookie to see who they are
if(isset($_COOKIE['_mkto_trk'])) {
// We only include the Marketo API class if it's needed.
include_once('class.marketoapi.php');
// What cookie type do we need to set?
$cookie_type = 'session';
$marketo_api = new MarketoAPI();
$result = $marketo_api->getLead('COOKIE', $_COOKIE['_mkto_trk']);
$data = $result->result->leadRecordList->leadRecord;
$whodat = array("Id" => $data->Id, "Email" => $data->Email);
// can we get a name?
foreach ($data->leadAttributeList->attribute as $attribute) {
//only record stuff we're truly interested in
if ((bool)array_search($attribute->attrName, array("{dummy}","FirstName","LastName","InferredCompany","InferredCountry","Company","Country"))) {
$whodat[$attribute->attrName] = $attribute->attrValue;
}
};
// Save a cookie containing the whodat data, so that there's no need to hit the Marketo API
// for future requests during this session. (expire = 0)
// Remember to configure cookie_domain in settings.php.
setcookie($name = $cookie, $value = json_encode($whodat), $expire = 0, $path = '/', $domain = $cookie_domain);
return json_encode(array(
"result" => true,
"from" => "marketo",
"data" => $whodat
));
}
else
{
return '{"result": true, "from": null, "data": null, "msg": "No Marketo cookie"}';
}
}
return '{"result": false}'; // tell the world that we found nothing.
}
?>