/**
  * Send an authenticated request to the Urban Airship API. The request is
  * authenticated with the key and secret.
  *
  * @param string  $method      REST method for request
  * @param mixed   $body        Body of request, optional
  * @param string  $uri         URI for this request
  * @param string  $contentType Content type for the request, optional
  * @param int     $version     version # for API, optional, default is 3
  * @param mixed   $request     Request object for this operation (PushRequest, etc)
  *   optional
  * @return \Httpful\associative|string
  * @throws AirshipException
  */
 public function request($method, $body, $uri, $contentType = null, $version = 3, $request = null)
 {
     // As a result of replacing Httpful/Request with WP HTTP,
     // We need to map WP_HTTP response object to Httpful/Request properties as a shim,
     // Action is not necessary but looks a bit cleaner
     add_action('http_api_debug', array($this, 'set_mock_response_object'), 10, 5);
     $headers = array('Authorization' => 'Basic ' . base64_encode("{$this->key}:{$this->secret}"), "Accept" => sprintf(self::VERSION_STRING, $version));
     if (!is_null($contentType)) {
         $headers["Content-type"] = $contentType;
     }
     $request = new \WP_Http();
     /**
      * Logger is disabled in production, so this won't do nothing unless WP_DEBUG is enabled
      *
      * @var [type]
      */
     $logger = UALog::getLogger();
     $logger->debug("Making request", array("method" => $method, "uri" => $uri, "headers" => $headers, "body" => $body));
     // Make a request (fires http_api_debug action that sets object property $mock_response)
     $response = $request->request($uri, array('method' => $method, 'body' => $body, 'headers' => $headers));
     // Check the response for wp_error (that's what WP HTTP throws when there was an issue with request)
     if (is_wp_error($response)) {
         return $response;
     }
     // Check for "successful" WP HTTP request and see if UA returns any non-2xx response code
     if (300 <= $response['response']['code']) {
         throw AirshipException::fromResponse($this->mock_response);
     }
     $logger->debug("Received response", array("status" => $this->mock_response->code, "headers" => $this->mock_response->raw_headers, "body" => $this->mock_response->raw_body));
     // Return mock response object for any components of UA library that make requests
     return $this->mock_response;
 }
 function send()
 {
     $uri = $this->airship->buildUrl(self::SCHEDULE_URL);
     $logger = UALog::getLogger();
     $response = $this->airship->request("POST", json_encode($this->getPayload()), $uri, "application/json", 3);
     $payload = json_decode($response->raw_body, true);
     $logger->info("Scheduled push sent successfully.", array("schedule_urls" => $payload['schedule_urls']));
     return new PushResponse($response);
 }
<?php

require_once __DIR__ . "/../../vendor/autoload.php";
use UrbanAirship\UALog;
use Monolog\Handler\NullHandler;
/*UALog::setLogHandlers(array(new StreamHandler("php://stdout", Logger::DEBUG)));*/
UALog::setLogHandlers(array(new NullHandler()));
 public function sendPushNotification(Notification $notification)
 {
     $sm = $this->getServiceLocator();
     $objectManager = $sm->get('Doctrine\\ORM\\EntityManager');
     $config = $sm->get('Config');
     if (!isset($config['urban-airship'])) {
         throw new \Exception('Urban Airship configuration is missing. Please make sure `config/autoload/urban-airship.local.php` exists in installation path.');
     } else {
         if (!isset($config['urban-airship']['api-key']) || $config['urban-airship']['api-key']) {
             throw new \Exception('Urban Airship API key is missing. Please make sure `config/autoload/urban-airship.local.php` exists in installation path and that all variables are set correctly.');
         } else {
             if (!isset($config['urban-airship']['api-secret']) || $config['urban-airship']['api-secret']) {
                 throw new \Exception('Urban Airship API secret is missing. Please make sure `config/autoload/urban-airship.local.php` exists in installation path and that all variables are set correctly.');
             }
         }
     }
     UALog::setLogHandlers(array(new StreamHandler('logs' . DIRECTORY_SEPARATOR . 'urban_airship.' . date('Y-m-d') . '.log', Logger::DEBUG)));
     $airship = new Airship($config['urban-airship']['api-key'], $config['urban-airship']['api-secret']);
     try {
         $response = $airship->push()->setAudience(P\deviceToken($device->getDeviceToken()))->setNotification(P\notification($message))->setDeviceTypes(P\deviceTypes($device->getDeviceType()))->send();
         if ($response->ok) {
             $notification->setStatus('sent');
             $notification->setOperationId($response->operation_id);
             $notification->setPushIds($response->push_ids);
         } else {
             $notification->setStatus('failed');
         }
         $objectManager->persist($notification);
         $objectManager->flush();
     } catch (AirshipException $e) {
         $error = $this->logError($e->getCode(), $e->getMessage(), __FILE__, __LINE__, $e->getTrace());
         $notification->setStatus('failed');
         $notification->setError($error);
         $objectManager->persist($notification);
         $objectManager->flush();
         throw $e;
     }
 }
<?php

require_once 'vendor/autoload.php';
use UrbanAirship\Airship;
use UrbanAirship\UALog;
use UrbanAirship\Push as P;
use Monolog\Logger;
use Monolog\Handler\StreamHandler;
UALog::setLogHandlers(array(new StreamHandler("php://stdout", Logger::INFO)));
$airship = new Airship("key", "secret");
$response = $airship->push()->setAudience(P\all)->setNotification(P\notification("Hello from PHP"))->setDeviceTypes(P\all)->send();
 /**
  * Send an authenticated request to the Urban Airship API. The request is
  * authenticated with the key and secret.
  *
  * @param string $method REST method for request
  * @param mixed $body Body of request, optional
  * @param string $uri URI for this request
  * @param string $contentType Content type for the request, optional
  * @param int $version version # for API, optional, default is 3
  * @param mixed $request Request object for this operation (PushRequest, etc)
  *   optional
  * @return \Httpful\associative|string
  * @throws AirshipException
  */
 public function request($method, $body, $uri, $contentType = null, $version = 3, $request = null)
 {
     $headers = array("Accept" => sprintf(self::VERSION_STRING, $version));
     if (!is_null($contentType)) {
         $headers["Content-type"] = $contentType;
     }
     $logger = UALog::getLogger();
     $logger->debug("Making request", array("method" => $method, "uri" => $uri, "headers" => $headers, "body" => $body));
     if (is_null($request)) {
         // Tests pass in a pre-built Request. Normal code builds one here.
         $request = Request::init();
     }
     $request->method($method)->uri($uri)->authenticateWith($this->key, $this->secret)->body($body)->addHeaders($headers);
     $response = $request->send();
     $logger->debug("Received response", array("status" => $response->code, "headers" => $response->raw_headers, "body" => $response->raw_body));
     if ($response->code >= 300) {
         throw AirshipException::fromResponse($response);
     }
     return $response;
 }