Overview

Namespaces

  • PushWoosh

Classes

  • PushWoosh
  • Overview
  • Namespace
  • Class
  • Tree
  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:      * @param string $apiToken The API token to use - Not to be used at the same time as the username and password
 45:      *
 46:      * @return PushWoosh
 47:      * @since 2014-02-27
 48:      * @author Matthew Daly matthew@astutech.com
 49:      */
 50:     public function __construct($appId, $username = null, $password = null, $apiToken = null)
 51:     {
 52:         // Set the config options up
 53:         $config = array();
 54:         $config['application'] = $appId;
 55:         $config['username'] = $username;
 56:         $config['password'] = $password;
 57:         $config['apitoken'] = $apiToken;
 58:         $this->config = $config;
 59:     }
 60: 
 61:     /**
 62:      * Sends a POST request to create the push message
 63:      *
 64:      * @param string $url The URL to send the POST request to
 65:      * @param string $data The data to be sent, encoded as JSON
 66:      * @param string $optional_headers Any optional headers. Defaults to null
 67:      *
 68:      * @return mixed Returns the response, or false if nothing received
 69:      * @since 2014-02-27
 70:      * @author Matthew Daly matthew@astutech.com
 71:      */
 72:     private function doPostRequest($url, $data, $optional_headers = null)
 73:     {
 74:         $params = array(
 75:             'http' => array(
 76:                 'method' => 'POST',
 77:                 'content' => $data
 78:             ));
 79:         if ($optional_headers !== null) {
 80:             $params['http']['header'] = $optional_headers;
 81:         }
 82:         $ctx = stream_context_create($params);
 83:         $fp = fopen($url, 'rb', false, $ctx);
 84:         if (!$fp) {
 85:             throw new Exception("Problem with $url, $php_errmsg");
 86:         }
 87: 
 88:         $response = @stream_get_contents($fp);
 89:         if ($response === false) {
 90:             return false;
 91:         }
 92:         return $response;
 93:     }
 94: 
 95:     /**
 96:      * Puts together the POST request to create the push message
 97:      *
 98:      * @param string $action The action to take
 99:      * @param array $data The data to send
100:      *
101:      * @return bool Confirms that the method executed
102:      * @since 2014-02-27
103:      * @author Matthew Daly matthew@astutech.com
104:      */
105:     private function pwCall($action, array $data = array())
106:     {
107:         $url = 'https://cp.pushwoosh.com/json/1.3/'.$action;
108:         $json = json_encode(array('request' => $data));
109:         $res = $this->doPostRequest($url, $json, 'Content-Type: application/json');
110:         $responseData = json_decode($res);
111:         if ($responseData->status_code == 200) {
112:             $response = true;
113:         } else {
114:             // Failed - log error and advise
115:             $response = false;
116:             error_log("Could not sent push - " . $responseData->status_message);
117:         }
118:         return $response;
119:     }
120: 
121:     /**
122:      * Creates a push message using PushWoosh
123:      *
124:      * @param array $pushes An array of messages to be sent. Each message in the array should be an associative array,
125:      * with the key 'content' representing the content of the message to be sent, and the key 'devices' representing 
126:      * the device token to send that message to. Leave 'devices' empty to send that message to all users
127:      * @param string $sendDate Send date of the message. Defaults to right now
128:      * @param string $link A link to follow when the push notification is clicked. Defaults to null
129:      * @param int $ios_badges The iOS badge number. Defaults to 1
130:      *
131:      * @return bool Confirms whether the method executed successfully
132:      * @since 2014-02-27
133:      * @author Matthew Daly matthew@astutech.com
134:      */
135:     public function createMessage(array $pushes, $sendDate = 'now', $link = null, $ios_badges = 0)
136:     {
137:         // Get the config settings
138:         $config = $this->config;
139: 
140:         // Store the message data
141:         if ($config['apitoken']) {
142:             $data = array(
143:                 'application' => $config['application'],
144:                 'auth' => $config['apitoken']
145:             );
146:         } else {
147:             $data = array(
148:                 'application' => $config['application'],
149:                 'username' => $config['username'],
150:                 'password' => $config['password']
151:             );
152:         }
153: 
154:         // Loop through each push and add them to the notifications array
155:         foreach ($pushes as $push) {
156:             $pushData = array(
157:                 'send_date' => $sendDate,
158:                 'content' => $push['content'],
159:                 'ios_badges' => $ios_badges
160:             );
161: 
162:             // If a list of devices is specified, add that to the push data
163:             if (array_key_exists('devices', $push)) {
164:                 $pushData['devices'] = $push['devices'];
165:             }
166: 
167:             // If a link is specified, add that to the push data
168:             if ($link) {
169:                 $pushData['link'] = $link;
170:             }
171: 
172:             // If a condition is set, add that to the push data
173:             if (isset($push['condition'])) {
174:                 $pushData['conditions'] = $push['conditions'];
175:             }
176: 
177:             // Add data to array
178:             $data['notifications'][] = $pushData;
179:         }
180: 
181:         // Send the message
182:         $response = $this->pwCall('createMessage', $data);
183: 
184:         // Return a value
185:         return $response;
186:     }
187: }
188: 
Pushwoosh PHP Library API documentation generated by ApiGen 2.8.0