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.1
25: * @since 2014-02-27
26: * @author Matthew Daly
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
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
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
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: var_dump($responseData);
110: if ($responseData->status_code == 200) {
111: $response = true;
112: } else {
113: // Failed - log error and advise
114: $response = false;
115: error_log("Could not sent push - " . $responseData->status_message);
116: }
117: return $response;
118: }
119:
120: /**
121: * Creates a push message using PushWoosh
122: *
123: * @param array $pushes An array of messages to be sent. Each message in the array should be an associative array,
124: * with the key 'content' representing the content of the message to be sent, and the key 'devices' representing
125: * the device token to send that message to. Leave 'devices' empty to send that message to all users
126: * @param string $sendDate Send date of the message. Defaults to right now
127: * @param string $link A link to follow when the push notification is clicked. Defaults to null
128: * @param int $ios_badges The iOS badge number. Defaults to 1
129: *
130: * @return bool Confirms whether the method executed successfully
131: * @since 2014-02-27
132: * @author Matthew Daly
133: */
134: public function createMessage(array $pushes, $sendDate = 'now', $link = null, $ios_badges = 1)
135: {
136: // Get the config settings
137: $config = $this->config;
138:
139: // Store the message data
140: $data = array(
141: 'application' => $config['application'],
142: 'username' => $config['username'],
143: 'password' => $config['password']
144: );
145:
146: // Loop through each push and add them to the notifications array
147: foreach ($pushes as $push) {
148: $pushData = array(
149: 'send_date' => $sendDate,
150: 'content' => $push['content'],
151: 'ios_badges' => $ios_badges
152: );
153:
154: // If a list of devices is specified, add that to the push data
155: if (array_key_exists('devices', $push)) {
156: $pushData['devices'] = $push['devices'];
157: }
158:
159: // If a link is specified, add that to the push data
160: if ($link) {
161: $pushData['link'] = $link;
162: }
163: $data['notifications'][] = $pushData;
164: }
165:
166: // Send the message
167: $response = $this->pwCall('createMessage', $data);
168:
169: // Return a value
170: return $response;
171: }
172: }
173: