1: <?php
2: /**
3: * A PHP library for sending push messages using PushWoosh.
4: *
5: * This library allows new push notifications to be created using the PushWoosh service.
6: *
7: * Can be installed using Composer.
8: *
9: * @package PushWoosh
10: */
11:
12: namespace PushWoosh;
13:
14: /**
15: * The PushWoosh class is a class for interacting with the PushWoosh push notification service.
16: *
17: * The constructor of the PushWoosh class takes parameters for the application ID, username and password.
18: * @category library
19: * @package PushWoosh
20: * @license http://opensource.org/licenses/MIT
21: * @example <br />
22: * $push = new PushWoosh($appId, $username, $password);<br />
23: * $push->createMessage($users, 'now', null);
24: * @version 0.0.2
25: * @since 2014-02-27
26: * @author Matthew Daly matthew@astutech.com
27: */
28:
29: class PushWoosh
30: {
31: /**
32: * The configuration settings. Should consist of an array with three keys:
33: * application, username and password
34: * @var array
35: */
36: protected $config;
37:
38: /**
39: * Constructor for the PushWoosh object
40: *
41: * @param string $appId The PushWoosh app ID to use
42: * @param string $username The PushWoosh username to use
43: * @param string $password The PushWoosh password to use
44: *
45: * @return PushWoosh
46: * @since 2014-02-27
47: * @author Matthew Daly matthew@astutech.com
48: */
49: public function __construct($appId, $username, $password)
50: {
51: // Set the config options up
52: $config = array();
53: $config['application'] = $appId;
54: $config['username'] = $username;
55: $config['password'] = $password;
56: $this->config = $config;
57: }
58:
59: /**
60: * Sends a POST request to create the push message
61: *
62: * @param string $url The URL to send the POST request to
63: * @param string $data The data to be sent, encoded as JSON
64: * @param string $optional_headers Any optional headers. Defaults to null
65: *
66: * @return mixed Returns the response, or false if nothing received
67: * @since 2014-02-27
68: * @author Matthew Daly matthew@astutech.com
69: */
70: private function doPostRequest($url, $data, $optional_headers = null)
71: {
72: $params = array(
73: 'http' => array(
74: 'method' => 'POST',
75: 'content' => $data
76: ));
77: if ($optional_headers !== null) {
78: $params['http']['header'] = $optional_headers;
79: }
80: $ctx = stream_context_create($params);
81: $fp = fopen($url, 'rb', false, $ctx);
82: if (!$fp) {
83: throw new Exception("Problem with $url, $php_errmsg");
84: }
85:
86: $response = @stream_get_contents($fp);
87: if ($response === false) {
88: return false;
89: }
90: return $response;
91: }
92:
93: /**
94: * Puts together the POST request to create the push message
95: *
96: * @param string $action The action to take
97: * @param array $data The data to send
98: *
99: * @return bool Confirms that the method executed
100: * @since 2014-02-27
101: * @author Matthew Daly matthew@astutech.com
102: */
103: private function pwCall($action, array $data = array())
104: {
105: $url = 'https://cp.pushwoosh.com/json/1.3/'.$action;
106: $json = json_encode(array('request' => $data));
107: $res = $this->doPostRequest($url, $json, 'Content-Type: application/json');
108: $responseData = json_decode($res);
109: if ($responseData->status_code == 200) {
110: $response = true;
111: } else {
112: // Failed - log error and advise
113: $response = false;
114: error_log("Could not sent push - " . $responseData->status_message);
115: }
116: return $response;
117: }
118:
119: /**
120: * Creates a push message using PushWoosh
121: *
122: * @param array $pushes An array of messages to be sent. Each message in the array should be an associative array,
123: * with the key 'content' representing the content of the message to be sent, and the key 'devices' representing
124: * the device token to send that message to. Leave 'devices' empty to send that message to all users
125: * @param string $sendDate Send date of the message. Defaults to right now
126: * @param string $link A link to follow when the push notification is clicked. Defaults to null
127: * @param int $ios_badges The iOS badge number. Defaults to 1
128: *
129: * @return bool Confirms whether the method executed successfully
130: * @since 2014-02-27
131: * @author Matthew Daly matthew@astutech.com
132: */
133: public function createMessage(array $pushes, $sendDate = 'now', $link = null, $ios_badges = 0)
134: {
135: // Get the config settings
136: $config = $this->config;
137:
138: // Store the message data
139: $data = array(
140: 'application' => $config['application'],
141: 'username' => $config['username'],
142: 'password' => $config['password']
143: );
144:
145: // Loop through each push and add them to the notifications array
146: foreach ($pushes as $push) {
147: $pushData = array(
148: 'send_date' => $sendDate,
149: 'content' => $push['content'],
150: 'ios_badges' => $ios_badges
151: );
152:
153: // If a list of devices is specified, add that to the push data
154: if (array_key_exists('devices', $push)) {
155: $pushData['devices'] = $push['devices'];
156: }
157:
158: // If a link is specified, add that to the push data
159: if ($link) {
160: $pushData['link'] = $link;
161: }
162:
163: // If a condition is set, add that to the push data
164: if (isset($push['condition'])) {
165: $pushData['conditions'] = $push['conditions'];
166: }
167:
168: // Add data to array
169: $data['notifications'][] = $pushData;
170: }
171:
172: // Send the message
173: $response = $this->pwCall('createMessage', $data);
174:
175: // Return a value
176: return $response;
177: }
178: }
179: