-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsubscription.php
157 lines (125 loc) · 5.06 KB
/
subscription.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
<?php
/* Work Flow
* 1. Check if the subscription exists.
* 2a. If if does exist, retrieve subscription_id.
* 2b. If it does NOT exist, skip.
* 3a. Edit the subscription and add to the new list.
* 3b. Add new subscription to list.
*/
include "request_rest.php";
function subscription($firstName, $lastName, $mobileNumber, $email) {
$getSubscriptionReturn = getSubscription($firstName, $lastName, $mobileNumber, $email);
$subscription_id = $getSubscriptionReturn['subscription_id_list'][0];
$error_message = $getSubscriptionReturn['error_message'];
error_log($subscription_id);
error_log("subscription_id");
error_log($error_message);
error_log("error message");
// it exists, post subscription
if(!is_null($subscription_id) && !empty($subscription_id)) {
$postSubscriptionReturn = postSubscription($firstName, $lastName, $mobileNumber, $email, $subscription_id);
error_log("postsub");
return $error_message = NULL;
}
// doesnt exist, put subscription
if(!is_null($error_message) && !empty($error_message)) {
putSubscription($firstName, $lastName, $mobileNumber, $email);
error_log("putsub");
return;
}
return;
}
function getSubscription($firstName, $lastName, $mobileNumber, $email) {
$request_url = "http://api.trumpia.com/rest/v1/vuskeedoo/subscription/search?"; // replace {username} (case sensitive)
$search_type = "2";
$search_data = $mobileNumber;
$list_id = "1623799";
$request_url = $request_url . "search_type=" . $search_type . "&search_data=" . $search_data . "&list_id=" . $list_id;
$request_rest = new RestRequest();
$request_rest->setRequestURL($request_url);
$request_rest->setAPIKey("7f5a72d074cff3222bfa0b079af236fc"); // replace {apikey} with assigned API key
$request_rest->setMethod("GET");
$result = $request_rest->execute();
$response_status = $result[0];
$json_response_data = $result[1];
// decode json
$json_data = json_decode($json_response_data, true);
error_log($request_url);
error_log("get subscription");
return $json_data;
}
function putSubscription($firstName, $lastName, $mobileNumber, $email) {
$request_url = "http://api.trumpia.com/rest/v1/vuskeedoo/subscription"; // replace {username} (case sensitive)
// Add a subscription
$request_data = array(
"list_name" => "Heroku",
"subscriptions" => array(
array(
"first_name" => $firstName,
"last_name" => $lastName,
"email" => $email,
"mobile" => array("number" => $mobileNumber, "country_code" => "1"),
"voice_device" => "mobile"
)
)
);
$request_rest = new RestRequest();
$request_rest->setRequestURL($request_url);
$request_rest->setAPIKey("7f5a72d074cff3222bfa0b079af236fc"); // replace {apikey} with assigned API key
$request_rest->setRequestBody(json_encode($request_data));
$request_rest->setMethod("PUT");
$result = $request_rest->execute();
$response_status = $result[0];
$json_response_data = $result[1];
//decode json into string
$json_data = json_decode($json_response_data, true);
$request_id = $json_data['request_id'];
$error_message = $json_data['error_message'];
error_log("put error");
error_log($error_message);
error_log("put id");
error_log($request_id);
getReport($request_id);
}
function postSubscription($firstName, $lastName, $mobileNumber, $subscription_id) {
$request_url = "http://api.trumpia.com/rest/v1/vuskeedoo/subscription"; // replace {username} (case sensitive)
$request_url = $request_url. "/". $subscription_id;
$request_data = array(
"list_name" => "Heroku",
"subscriptions" => array(
array(
"first_name" => $firstName,
"last_name" => $lastName,
"email" => "$email",
"mobile" => array($mobileNumber, "country_code" => "1"),
"voice_device" => "mobile"
)
)
);
$request_rest = new RestRequest();
$request_rest->setRequestURL($request_url);
$request_rest->setAPIKey("7f5a72d074cff3222bfa0b079af236fc"); // replace {api_key} with assigned API Key
$request_rest->setRequestBody(json_encode($request_data));
$request_rest->setMethod("POST");
$result = $request_rest->execute();
$response_status = $result[0];
$json_response_data = $result[1];
}
function getReport($request_id) {
$request_url = "http://api.trumpia.com/rest/v1/vuskeedoo/report"; // replace {username} (case sensitive)
$request_url = $request_url . "/" . $request_id;
$request_rest = new RestRequest();
$request_rest->setRequestURL($request_url);
$request_rest->setAPIKey("7f5a72d074cff3222bfa0b079af236fc"); // replace {apikey} with assigned API key
$request_rest->setMethod("GET");
$result = $request_rest->execute();
$response_status = $result[0];
$json_response_data = $result[1];
// decode json into string
$json_data = json_decode($json_response_data, true);
$error_message = $json_data['error_message'];
$subscription_id = $json_data['subscription_id'];
error_log("get report error");
error_log($error_message);
}
?>