This repository was archived by the owner on Oct 13, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRaveRestAPI.php
More file actions
1243 lines (984 loc) · 33.8 KB
/
RaveRestAPI.php
File metadata and controls
1243 lines (984 loc) · 33.8 KB
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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
/**
* PHP/curl client for the RAVE wireless (http://www.getrave.com) User Management REST API
* Please see the Rave User Management API Reference Guide for details on the API
*
* @package RaveRestAPI
* @author epierce
* @version 1.1
*
*/
class RaveRestAPI
{
/**
* Stores an error string for the last request if one occurred
*
* @var string
* @access protected
*/
protected $error = '';
/**
* Curl object used to connect to web service
* @var string
* @access protected
*/
protected $curl;
/**
* Turn on debug output
*
* @var boolean
* @access protected
*/
protected $debug;
/**
* Intializes a RaveRestAPI object
*
* @param string $user
* @param string $password
* @param string $REST_URL
*/
public function __construct($user,$password,$REST_URL)
{
$this->curl = new Curl;
$this->debug = false;
$this->user = $user;
$this->password = $password;
$this->REST_URL = $REST_URL;
$this->setCommonCurlOptions();
}
/**
* Turns on/off debugging output
*
* @param boolean $value
*/
public function setDebug($value = true)
{
$this->debug = $value;
}
/**
* get error string from last web service request
* @return string
*/
public function getError()
{
return $this->error;
}
/**
* Prints debugging information from the Curl object
*
* @param string $function
* @param string $request_URL
* @param string $payload
* @param object $response
*/
private function printResponseDebug($function,$request_URL,$payload,$response)
{
echo "=======================\n";
echo " $function \n";
echo "=======================\n";
echo "Connecting to $request_URL \n";
echo "Payload: $payload\n";
echo "Response: Headers\n";
print_r($response->headers);
echo "Response: Body\n";
print_r($response->body);
echo "\n=======================\n";
return;
}
/**
* Handle HTTP error codes from the Curl object
*
* @param object $response
*/
private function errorHandler($response)
{
switch ($response->headers['Status-Code']) {
case 401: $this->error = 'Authentication Failure: Username or Password Incorrect';
break;
case 406: $xml = simplexml_load_string($response->body);
$this->error = 'Input data error: '.$xml->errorMessage;
break;
case 500: $xml = simplexml_load_string($response->body);
$this->error = 'Internal Server Error: '.$xml->errorMessage;
break;
}
if($this->debug) echo $this->error." \n";
return;
}
/**
* Set the associated CURL options for a RAVE API request
*/
private function setCommonCurlOptions()
{
$USERPWD = $this->user.':'.$this->password;
$this->curl->options['CURLOPT_RETURNTRANSFER'] = 1;
//This didn't work for POST/PUT - forced the correct value in curl->request()
$this->curl->options['CURLOPT_HTTPHEADER'] = array('Content-type: application/xml; charset=utf-8');
$this->curl->options['CURLOPT_HTTPAUTH'] = 'CURLAUTH_BASIC';
$this->curl->options['CURLOPT_USERPWD'] = $USERPWD;
}
/**
* Gets the Cell Phone Carrier for a given phone number
*
* $PhoneNumber must be 10 digits
*
* Returns an assoc. array with elements:
* Name : Carrier Name
* ID : Carrier ID number - use this number when interacting with other RAVE services
*
* Returns 0 on failure
*
* @param number $PhoneNumber
* @return number|array
*/
public function lookupCarrier($PhoneNumber)
{
$request_URL = $this->REST_URL."siteadmin/mobilecarriers/phonecarrier/".$PhoneNumber;
$this->setCommonCurlOptions();
$response = $this->curl->get($request_URL);
if($this->debug) $this->printResponseDebug('lookupCarrier',$request_URL,'',$response);
//Valid Return Codes for REST API are 200 or 202
if (($response->headers['Status-Code'] != 200) && ($response->headers['Status-Code'] != 202)) {
$this->errorHandler($response);
return 0;
}
$xml = simplexml_load_string($response);
$carrier['ID'] = (int) $xml->attributes()->id;
$carrier['Name'] = (String) $xml->name;
return $carrier;
}
/**
* Sends a SMS confirmation code to a given RAVE subscriber
*
* $Email must already be registered with RAVE and have a Mobile Phone Number set
*
* @param string $Email
* @return boolean
*/
public function sendConfCode($Email)
{
$request_URL = $this->REST_URL."user/".$Email."/sendconfcode";
$this->setCommonCurlOptions();
$response = $this->curl->post($request_URL);
if($this->debug) $this->printResponseDebug('sendConfCode',$request_URL,'',$response);
//Valid Return Codes for REST API are 200 or 202
if (($response->headers['Status-Code'] != 200) && ($response->headers['Status-Code'] != 202)) {
$this->errorHandler($response);
return 0;
}
return 1;
}
/**
* Sends confirmation code back to RAVE to confirm the mobile phone number for a subscriber
*
* @param string $Email
* @param number $ConfCode
* @return boolean
*/
public function confirmPhone($Email,$ConfCode)
{
$request_URL = $this->REST_URL."user/".$Email."/confirmphone";
$this->setCommonCurlOptions();
$response = $this->curl->post($request_URL,$ConfCode);
if($this->debug) $this->printResponseDebug('confirmPhone',$request_URL,$ConfCode,$response);
//Valid Return Codes for REST API are 200 or 202
if (($response->headers['Status-Code'] != 200) && ($response->headers['Status-Code'] != 202)) {
$this->errorHandler($response);
return 0;
}
return 1;
}
/**
* Gets Rave User Data using Student Information System ID (sisID)
*
* @param string $sisID
* @return array|number
*/
public function findUserBySisId($sisID)
{
$request_URL = $this->REST_URL."user/findbysisid?sisid=".$sisID;
$this->setCommonCurlOptions();
$response = $this->curl->get($request_URL);
if($this->debug) $this->printResponseDebug('findUserBySisId',$request_URL,'',$response);
//Valid Return Codes for REST API are 200 or 202
if (($response->headers['Status-Code'] != 200) && ($response->headers['Status-Code'] != 202)) {
$this->errorHandler($response);
return 0;
}
return (array) simplexml_load_string($response);
}
/**
* Gets Rave User Data using Email
*
* @param string $Email
* @return array|number
*/
public function findUserByEmail($Email)
{
$request_URL = $this->REST_URL."user/".$Email;
$this->setCommonCurlOptions();
$response = $this->curl->get($request_URL);
if($this->debug) $this->printResponseDebug('findUserByEmail',$request_URL,'',$response);
//Valid Return Codes for REST API are 200 or 202
if (($response->headers['Status-Code'] != 200) && ($response->headers['Status-Code'] != 202)) {
$this->errorHandler($response);
return 0;
}
return (array) simplexml_load_string($response);
}
/**
* Updates Primary Email on a RAVE subscriber using the SISid as the identifier
*
* @param string $sisid
* @param string $Email
* @return boolean
*/
public function updatePrimaryEmail($sisid,$Email)
{
$request_URL = $this->REST_URL."user/updateprimaryemail?sisid=".$sisid."&email=".$Email;
$this->setCommonCurlOptions();
$response = $this->curl->post($request_URL, $vars);
if($this->debug) $this->printResponseDebug('updateUserPrimaryEmail',$request_URL,'',$response);
//Valid Return Codes for REST API are 200 or 202
if (($response->headers['Status-Code'] != 200) && ($response->headers['Status-Code'] != 202)) {
$this->errorHandler($response);
return 0;
}
return 1;
}
/**
* Creates a new RAVE subscriber
*
* @param string $firstName Subscriber's first name
* @param string $lastName Subscriber's last name
* @param string $Email Subscriber's primary Email address
* @param integer $mobileNumber1 Subscriber's cell phone number
* @param integer $mobileCarrier1 RAVE wireless CarrierID from lookupCarrier()
* @param boolean $mobile1Confirmed Indicates the number has been verified ('true'/'false' only - other forms of boolean not accepted)
* @param boolean $useMobile1ForVoice Indicates the number should be used for voice alerts ('true'/'false' only - other forms of boolean not accepted)
* @param string $sisId Alternate identifier for subscriber
* @param string $ssoId username for CAS/LDAP authentication
* @param string $languagePreference Preferred Language (default: 'en')
* @param string $administrationRole Supported Values: 'USER','BROADCAST_ADMIN','SITE_ADMIN' (default: 'USER')
* @param string $institutionRole
* @param string $userAttribute1
* @param string $userAttribute2
* @param string $userAttribute3
* @param string $userAttribute4
* @return boolean
*/
public function registerUser($firstName, $lastName, $Email, $mobileNumber1, $mobileCarrier1, $mobile1Confirmed, $useMobile1ForVoice, $sisId, $ssoId,
$languagePreference='en', $administrationRole='USER', $institutionRole='', $userAttribute1='', $userAttribute2='', $userAttribute3='', $userAttribute4='')
{
$request_URL = $this->REST_URL."user";
$this->setCommonCurlOptions();
$raveUser = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8" ?><raveUser/>');
$raveUser->addChild('firstName', $firstName);
$raveUser->addChild('lastName', $lastName);
$raveUser->addChild('email', $Email);
$raveUser->addChild('sisId', $sisId);
$raveUser->addChild('ssoId', $ssoId);
$raveUser->addChild('languagePreference', $languagePreference);
$raveUser->addChild('administrationRole', $administrationRole);
$raveUser->addChild('mobileNumber1', $mobileNumber1);
$raveUser->addChild('mobileCarrier1', $mobileCarrier1);
$raveUser->addChild('mobile1Confirmed', $mobile1Confirmed);
$raveUser->addChild('useMobile1ForVoice', $useMobile1ForVoice);
$raveUser->addChild('institutionRole', $institutionRole);
$raveUser->addChild('userAttribute1', $userAttribute1);
$raveUser->addChild('userAttribute2', $userAttribute2);
$raveUser->addChild('userAttribute3', $userAttribute3);
$raveUser->addChild('userAttribute4', $userAttribute4);
$POST_Payload = $raveUser->asXML();
$response = $this->curl->post($request_URL, $POST_Payload);
if($this->debug) $this->printResponseDebug('registerUser',$request_URL,$POST_Payload,$response);
//Valid Return Codes for REST API are 200 or 202
if (($response->headers['Status-Code'] != 200) && ($response->headers['Status-Code'] != 202)) {
$this->errorHandler($response);
return 0;
}
return 1;
}
/**
* Updates RAVE subscriber record
*
* @param string $firstName Subscriber's first name
* @param string $lastName Subscriber's last name
* @param string $Email Subscriber's primary Email address
* @param integer $mobileNumber1 Subscriber's cell phone number
* @param integer $mobileCarrier1 RAVE wireless CarrierID from lookupCarrier()
* @param boolean $mobile1Confirmed Indicates the number has been verified ('true'/'false' only - other forms of boolean not accepted)
* @param boolean $useMobile1ForVoice Indicates the number should be used for voice alerts ('true'/'false' only - other forms of boolean not accepted)
* @param string $sisId Alternate identifier for subscriber
* @param string $ssoId username for CAS/LDAP authentication
* @param string $languagePreference Preferred Language (default: 'en')
* @param string $administrationRole Supported Values: 'USER','BROADCAST_ADMIN','SITE_ADMIN' (default: 'USER')
* @param string $institutionRole
* @param string $userAttribute1
* @param string $userAttribute2
* @param string $userAttribute3
* @param string $userAttribute4
* @return boolean
*/
public function updateUser($firstName, $lastName, $Email, $mobileNumber1, $mobileCarrier1, $mobile1Confirmed, $useMobile1ForVoice, $sisId, $ssoId,
$languagePreference='en', $administrationRole='USER', $institutionRole='', $userAttribute1='', $userAttribute2='', $userAttribute3='', $userAttribute4='')
{
$request_URL = $this->REST_URL."user";
$this->setCommonCurlOptions();
$raveUser = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8" ?><raveUser/>');
$raveUser->addChild('firstName', $firstName);
$raveUser->addChild('lastName', $lastName);
$raveUser->addChild('email', $Email);
$raveUser->addChild('sisId', $sisId);
$raveUser->addChild('ssoId', $ssoId);
$raveUser->addChild('languagePreference', $languagePreference);
$raveUser->addChild('administrationRole', $administrationRole);
$raveUser->addChild('mobileNumber1', $mobileNumber1);
$raveUser->addChild('mobileCarrier1', $mobileCarrier1);
$raveUser->addChild('mobile1Confirmed', $mobile1Confirmed);
$raveUser->addChild('useMobile1ForVoice', $useMobile1ForVoice);
$raveUser->addChild('institutionRole', $institutionRole);
$raveUser->addChild('userAttribute1', $userAttribute1);
$raveUser->addChild('userAttribute2', $userAttribute2);
$raveUser->addChild('userAttribute3', $userAttribute3);
$raveUser->addChild('userAttribute4', $userAttribute4);
$PUT_Payload = $raveUser->asXML();
$response = $this->curl->put($request_URL, $PUT_Payload);
if($this->debug) $this->printResponseDebug('updateUser',$request_URL,$PUT_Payload,$response);
//Valid Return Codes for REST API are 200 or 202
if (($response->headers['Status-Code'] != 200) && ($response->headers['Status-Code'] != 202)) {
$this->errorHandler($response);
return 0;
}
return 1;
}
/**
* Updates RAVE password -- Not needed for CAS/LDAP authentication
*
* @param string $Email
* @param string $Password
* @return boolean
*/
public function updatePassword($Email,$Password)
{
$request_URL = $this->REST_URL."user/".$Email."/resetpassword";
$this->setCommonCurlOptions();
$response = $this->curl->post($request_URL, $Password);
if($this->debug) $this->printResponseDebug('updatePassword',$request_URL,$Password,$response);
//Valid Return Codes for REST API are 200 or 202
if (($response->headers['Status-Code'] != 200) && ($response->headers['Status-Code'] != 202)) {
$this->errorHandler($response);
return 0;
}
return 1;
}
/**
* Removes RAVE subscriber
*
* @param string $Email
* @return boolean
*/
public function deleteUser($Email)
{
$request_URL = $this->REST_URL."user/".$Email;
$this->setCommonCurlOptions();
$response = $this->curl->delete($request_URL);
if($this->debug) $this->printResponseDebug('deleteUser',$request_URL,'',$response);
//Valid Return Codes for REST API are 200 or 202
if (($response->headers['Status-Code'] != 200) && ($response->headers['Status-Code'] != 202)) {
$this->errorHandler($response);
return 0;
}
return 1;
}
/**
* Get all subscribed groups for a single RAVE user
*
* returns assoc.array with elements:
*
* $membership[0]['groupID'] : RAVE GroupId number
* $membership[0]['groupURL'] : Web Service URL to get details/modify group
* $membership[0]['role'] : Subscriber's role in this group
*
* Returns 0 on failure
*
* @param string $Email
* @return number|array
*/
public function getSubscribedGroupsForUser($Email)
{
$request_URL = $this->REST_URL."user/".$Email."/groups";
$this->setCommonCurlOptions();
$response = $this->curl->get($request_URL);
if($this->debug) $this->printResponseDebug('getSubscribedGroupsForUser',$request_URL,'',$response);
//Valid Return Codes for REST API are 200 or 202
if (($response->headers['Status-Code'] != 200) && ($response->headers['Status-Code'] != 202)) {
$this->errorHandler($response);
return 0;
}
$xml = simplexml_load_string($response);
foreach ($xml->raveGroupMembership as $Membership) {
$groupID = $Membership->attributes()->groupId;
$groupURL = $Membership->attributes()->groupDetailsURL;
$role = $Membership->role;
$membership[] = array('groupID' => (int) $groupID,'groupURL' => (string) $groupURL,'role' => (string) $role);
}
return $membership;
}
/**
* Subscribe User to a RAVE Group
*
* @param integer $groupId RAVE groupID
* @param string $Email Subscriber's primary email address
* @param boolean $alertByPhone Indicates if the subscriber should be notified via voice ('true'/'false' only - other forms of boolean not accepted)
* @param boolean $alertByEmail Indicates if the subscriber should be notified via email ('true'/'false' only - other forms of boolean not accepted)
* @param string $role Supported Values: 'ADMIN', 'CONTRIBUTOR', 'MEMBER' (default: 'MEMBER')
* @return boolean
*/
public function subscribeToGroup($groupId,$Email,$alertByPhone='true',$alertByEmail='true',$role='MEMBER')
{
$request_URL = $this->REST_URL."user/".$Email."/groups";
$this->setCommonCurlOptions();
$raveGroupMembership = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8" ?><raveGroupMembership/>');
$raveGroupMembership->addAttribute('groupId',$groupId);
$raveGroupMembership->addChild('alertByPhone',$alertByPhone);
$raveGroupMembership->addChild('alertByEmail',$alertByEmail);
$raveGroupMembership->addChild('role',$role);
$POST_Payload = $raveGroupMembership->asXML();
$response = $this->curl->post($request_URL, $POST_Payload);
if($this->debug) $this->printResponseDebug('subscribeToGroup',$request_URL,$POST_Payload,$response);
//Valid Return Codes for REST API are 200 or 202
if (($response->headers['Status-Code'] != 200) && ($response->headers['Status-Code'] != 202)) {
$this->errorHandler($response);
return 0;
}
return 1;
}
/**
* Update user subscription to a RAVE Group
*
* @param integer $groupId RAVE groupID
* @param string $Email Subscriber's primary email address
* @param boolean $alertByPhone Indicates if the subscriber should be notified via voice ('true'/'false' only - other forms of boolean not accepted)
* @param boolean $alertByEmail Indicates if the subscriber should be notified via email ('true'/'false' only - other forms of boolean not accepted)
* @param string $role Supported Values: 'ADMIN', 'CONTRIBUTOR', 'MEMBER' (default: 'MEMBER')
* @return boolean
*/
public function updateGroupSubscription($groupId,$Email,$alertByPhone='true',$alertByEmail='true',$role='MEMBER')
{
$request_URL = $this->REST_URL."user/".$Email."/groups";
$this->setCommonCurlOptions();
$raveGroupMembership = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8" ?><raveGroupMembership/>');
$raveGroupMembership->addAttribute('groupId',$groupId);
$raveGroupMembership->addChild('alertByPhone',$alertByPhone);
$raveGroupMembership->addChild('alertByEmail',$alertByEmail);
$raveGroupMembership->addChild('role',$role);
$PUT_Payload = $raveGroupMembership->asXML();
$response = $this->curl->put($request_URL, $PUT_Payload);
if($this->debug) $this->printResponseDebug('updateGroupSubscription',$request_URL,$PUT_Payload,$response);
//Valid Return Codes for REST API are 200 or 202
if (($response->headers['Status-Code'] != 200) && ($response->headers['Status-Code'] != 202)) {
$this->errorHandler($response);
return 0;
}
return 1;
}
/**
* Unsubscribe from a RAVE group
*
* @param integer $groupId RAVE GroupID
* @param string $Email Subscriber's primary email address
* @return boolean
*/
public function unsubscribeToGroup($groupId,$Email)
{
$request_URL = $this->REST_URL."user/".$Email."/groups/".$groupId;
$this->setCommonCurlOptions();
$response = $this->curl->delete($request_URL);
if($this->debug) $this->printResponseDebug('unsubscribeToGroup',$request_URL,'',$response);
//Valid Return Codes for REST API are 200 or 202
if (($response->headers['Status-Code'] != 200) && ($response->headers['Status-Code'] != 202)) {
$this->errorHandler($response);
return 0;
}
return 1;
}
/**
* Get all subscribed lists for a single RAVE user
*
* returns array of the ListIDs this user is subscribed to
*
* Returns 0 on failure
*
* @param string $Email
* @return number|array
*/
public function getSubscribedListsForUser($Email)
{
$request_URL = $this->REST_URL."user/".$Email."/userlists";
$this->setCommonCurlOptions();
$response = $this->curl->get($request_URL);
if($this->debug) $this->printResponseDebug('getSubscribedListsForUser',$request_URL,'',$response);
//Valid Return Codes for REST API are 200 or 202
if (($response->headers['Status-Code'] != 200) && ($response->headers['Status-Code'] != 202)) {
$this->errorHandler($response);
return 0;
}
$xml = simplexml_load_string($response);
$raveListMemberships = array();
foreach ($xml->xpath('//userList') as $Membership) {
$raveListMemberships[] = (int) $Membership->attributes()->id;
}
return $raveListMemberships;
}
/**
* Subscribe user to a RAVE List
* TODO: find out the difference between 'group' & 'list' in RAVE
*
* @param integer $listId RAVE List ID number
* @param string $Email Subscriber's primary email address
* @return boolean
*/
public function subscribeToList($listId,$Email)
{
$request_URL = $this->REST_URL."user/".$Email."/userlists/".$listId;
$this->setCommonCurlOptions();
$response = $this->curl->post($request_URL);
if($this->debug) $this->printResponseDebug('subscribeToList',$request_URL,'',$response);
//Valid Return Codes for REST API are 200 or 202
if (($response->headers['Status-Code'] != 200) && ($response->headers['Status-Code'] != 202)) {
$this->errorHandler($response);
return 0;
}
return 1;
}
/**
* Unsubscribe from a RAVE list
* @param integer $listId RAVE list ID number
* @param string $Email Subscriber's primary email address
* @return boolean
*/
public function unsubscribeToList($listId,$Email)
{
$request_URL = $this->REST_URL."user/".$Email."/userlists/".$listId;
$this->setCommonCurlOptions();
$response = $this->curl->delete($request_URL);
if($this->debug) $this->printResponseDebug('unsubscribeToList',$request_URL,'',$response);
//Valid Return Codes for REST API are 200 or 202
if (($response->headers['Status-Code'] != 200) && ($response->headers['Status-Code'] != 202)) {
$this->errorHandler($response);
return 0;
}
return 1;
}
/**
* List Administration
*/
/**
* Creates a new RAVE list
*
* @param string $listName List's name
* @param array $listMembers Array of members for this list
* @return array Array of members that were rejected by RAVE (i.e. email address do not exist)
*/
public function createUserList($listName, $listMembers)
{
$request_URL = $this->REST_URL."siteadmin/userlists";
$this->setCommonCurlOptions();
$raveList = new SimpleXMLElement('<?xml version="1.0" encoding="UTF-8" ?><userList/>');
$raveList->addChild('name', $listName);
$raveListMembers = $raveList->addChild('memberList');
foreach($listMembers as $member){
$raveListMembers->addChild('listMember', $member);
}
$POST_Payload = $raveList->asXML();
$response = $this->curl->post($request_URL, $POST_Payload);
if($this->debug) $this->printResponseDebug('createUserList',$request_URL,$POST_Payload,$response);
//Valid Return Codes for REST API are 200 or 202
if (($response->headers['Status-Code'] != 200) && ($response->headers['Status-Code'] != 202)) {
$this->errorHandler($response);
return 0;
}
$xml = simplexml_load_string($response);
$rejectedMembers = array();
foreach ($xml->xpath('//listMember') as $rejectedMember) {
$rejectedMembers[] = (string) $rejectedMember;
}
return $rejectedMembers;
}
/**
* Remove RAVE list
*
* @param int $listID
* @return boolean
*/
public function deleteUserList($listID)
{
$request_URL = $this->REST_URL."siteadmin/userlists/".$listID;
$this->setCommonCurlOptions();
$response = $this->curl->delete($request_URL);
if($this->debug) $this->printResponseDebug('deleteUserList',$request_URL,'',$response);
//Valid Return Codes for REST API are 200 or 202
if (($response->headers['Status-Code'] != 200) && ($response->headers['Status-Code'] != 202)) {
$this->errorHandler($response);
return 0;
}
return 1;
}
/**
* Get all RAVE lists
*
* returns assoc.array with elements:
*
* $raveList[0]['listID'] : RAVE ListId number
* $raveList[0]['listURL'] : Web Service URL to get all subscribed memebers
* $raveList[0]['name'] : List name
*
* Returns 0 on failure
*
* @return number|array
*/
public function getUserLists()
{
$request_URL = $this->REST_URL."siteadmin/userlists";
$this->setCommonCurlOptions();
$response = $this->curl->get($request_URL);
if($this->debug) $this->printResponseDebug('getUserLists',$request_URL,'',$response);
//Valid Return Codes for REST API are 200 or 202
if (($response->headers['Status-Code'] != 200) && ($response->headers['Status-Code'] != 202)) {
$this->errorHandler($response);
return 0;
}
$xml = simplexml_load_string($response);
foreach ($xml->userList as $userList) {
$listID = $userList->attributes()->id;
$listURL = $userList->attributes()->userListDetailsURL;
$name = $userList->name;
$raveList[] = array('listID' => (int) $listID,'listURL' => (string) $listURL,'name' => (string) $name);
}
return $raveList;
}
/**
* Get members of a RAVE list
*
* Returns 0 on failure
*
* @param int $listID Rave ListID number
* @return number|array Member list
*/
public function getUserListDetails($listID)
{
$request_URL = $this->REST_URL."siteadmin/userlists/".$listID;
$this->setCommonCurlOptions();
$response = $this->curl->get($request_URL);
if($this->debug) $this->printResponseDebug('getUserListDetails',$request_URL,'',$response);
//Valid Return Codes for REST API are 200 or 202
if (($response->headers['Status-Code'] != 200) && ($response->headers['Status-Code'] != 202)) {
$this->errorHandler($response);
return 0;
}
$xml = simplexml_load_string($response);
foreach ($xml->memberList->listMember as $listMember) {
$memberList[] = (string) $listMember;
}
return $memberList;
}
}
/**
* A basic CURL wrapper
*
* See the README for documentation/examples or http://php.net/curl for more information about the libcurl extension for PHP
*
* @package curl
* @author Sean Huber <shuber@huberry.com>
**/
class Curl {
/**
* The file to read and write cookies to for requests
*
* @var string
**/
public $cookie_file;
/**
* Determines whether or not requests should follow redirects
*
* @var boolean
**/
public $follow_redirects = true;
/**
* An associative array of headers to send along with requests
*
* @var array
**/
public $headers = array();
/**
* An associative array of CURLOPT options to send along with requests
*
* @var array
**/
public $options = array();
/**
* The referer header to send along with requests
*
* @var string
**/
public $referer;
/**
* The user agent to send along with requests
*
* @var string
**/
public $user_agent;
/**
* Stores an error string for the last request if one occurred
*
* @var string
* @access protected
**/
protected $error = '';
/**
* Stores resource handle for the current CURL request
*
* @var resource
* @access protected
**/
protected $request;
/**
* Initializes a Curl object
*
* Sets the $cookie_file to "curl_cookie.txt" in the current directory
* Also sets the $user_agent to $_SERVER['HTTP_USER_AGENT'] if it exists, 'Curl/PHP '.PHP_VERSION.' (http://github.com/shuber/curl)' otherwise
**/
function __construct() {
$this->cookie_file = dirname(__FILE__).DIRECTORY_SEPARATOR.'curl_cookie.txt';
$this->user_agent = isset($_SERVER['HTTP_USER_AGENT']) ? $_SERVER['HTTP_USER_AGENT'] : 'Curl/PHP '.PHP_VERSION.' (http://github.com/shuber/curl)';
}
/**
* Makes an HTTP DELETE request to the specified $url with an optional array or string of $vars
*
* Returns a CurlResponse object if the request was successful, false otherwise
*
* @param string $url
* @param array|string $vars
* @return CurlResponse object
**/
function delete($url, $vars = array()) {
return $this->request('DELETE', $url, $vars);
}
/**
* Returns the error string of the current request if one occurred