-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.php
85 lines (51 loc) · 2.28 KB
/
api.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
/*
This is an example intended to show how to access the Lotame ReST API.
We use simple classes available in the PHP 5.3.6 distro for simplicity
There may be more efficient ways to parse the XML and JSON, especially using other
libraries, but that is not the focus of the example.
This example also does not include an error handling - You should
make sure that you do include error handling with any code, but especially
when working with an external API!
*/
// make sure you install php5-curl and libxml
// Set up our username and password
$username = '[email protected]';
$password = 'yomismo';
// set up our request to get the token
$restUrl = 'https://api.lotame.com/';
// urlencode the post args
$postargs = 'email='.urlencode($username).'&password='.urlencode($password);
// initialize the curl session
$session = curl_init($restUrl);
// set up our curl options for posting the args
curl_setopt($session, CURLOPT_POST, true);
curl_setopt($session, CURLOPT_POSTFIELDS, $postargs);
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
// run curl, get the token
$token = curl_exec($session);
curl_close($session);
// You can see the form of the token here
// set our REST url to get what we want, in this case an audience list
$restUrl = "https://api.lotame.com/as/audiences";
// Initiate the session with the new end point
$session = curl_init($restUrl);
// Add the token to the header
//first let's update the header
curl_setopt($session,CURLOPT_HTTPHEADER,array("Authorization: $token","Accept: application/json"));
curl_setopt($session, CURLOPT_RETURNTRANSFER, true);
//Make our request
$jsonResponse = curl_exec($session);
// You can see the JSON we get back
echo ("$jsonResponse");
/*
Lets's decode our string and then loop through, getting our
audiences. We are treating everything as an array.
If we had wanted to work with objects instead we would use
$jsonAudienceList = json_decode($jsonResponse);
instead and then loop through objects instead of arrays.
*/
$jsonAudienceList = json_decode($jsonResponse, true);
// close the session
curl_close($session);
?>