1+ <?php
2+
3+ namespace Sendpulse \RestApi ;
4+
5+ use Exception ;
6+
7+ class Automation360
8+ {
9+ /**
10+ * @var string
11+ */
12+ private $ eventHash ;
13+ /**
14+ * @var string
15+ */
16+ private $ eventDomain = 'https://events.sendpulse.com/events/id/ ' ;
17+
18+ /**
19+ * Automation360 constructor.
20+ * @param string $eventHash
21+ * @throws Exception
22+ */
23+ public function __construct ($ eventHash )
24+ {
25+ if (!$ eventHash ) {
26+ throw new Exception ('Invalid parameter $eventHash ' , 500 );
27+ }
28+ $ this ->eventHash = $ eventHash ;
29+ }
30+
31+ /**
32+ * One of the variables $email or $phone
33+ * must necessarily contain the correct value
34+ *
35+ * @param null|string $email
36+ * @param null|string $phone
37+ * @param array $variables
38+ * @return array
39+ * @throws Exception
40+ */
41+ public function sendEventToSendpulse ($ email = null , $ phone = null , array $ variables = [])
42+ {
43+ if (!$ email && !$ phone ) {
44+ throw new \Exception ('Variables $email and $phone is empty ' , 500 );
45+ }
46+ if ($ email ) {
47+ $ variables ['email ' ] = $ email ;
48+ }
49+ if ($ phone ) {
50+ $ variables ['phone ' ] = $ phone ;
51+ }
52+ return $ this ->sendRequest ($ variables );
53+ }
54+
55+ /**
56+ * Send request to SendPulse with CURL
57+ *
58+ * @param array $variables
59+ * @return array
60+ */
61+ private function sendRequest (array $ variables )
62+ {
63+ $ url = $ this ->eventDomain . $ this ->eventHash ;
64+
65+ $ curl = curl_init ();
66+ curl_setopt ($ curl , CURLOPT_POST , count ($ variables ));
67+ curl_setopt ($ curl , CURLOPT_POSTFIELDS , http_build_query ($ variables ));
68+ curl_setopt ($ curl , CURLOPT_URL , $ url );
69+ curl_setopt ($ curl , CURLOPT_SSL_VERIFYPEER , false );
70+ curl_setopt ($ curl , CURLOPT_SSL_VERIFYHOST , false );
71+ curl_setopt ($ curl , CURLOPT_RETURNTRANSFER , true );
72+ curl_setopt ($ curl , CURLOPT_HEADER , true );
73+ curl_setopt ($ curl , CURLOPT_CONNECTTIMEOUT , 15 );
74+ curl_setopt ($ curl , CURLOPT_TIMEOUT , 15 );
75+ $ response = curl_exec ($ curl );
76+ $ header_size = curl_getinfo ($ curl , CURLINFO_HEADER_SIZE );
77+ $ headerCode = curl_getinfo ($ curl , CURLINFO_HTTP_CODE );
78+ $ responseBody = substr ($ response , $ header_size );
79+ curl_close ($ curl );
80+
81+ $ result = [
82+ 'http_code ' => $ headerCode ,
83+ 'data ' => json_decode ($ responseBody , true )
84+ ];
85+
86+ return $ result ;
87+ }
88+ }
0 commit comments