public function sendPushNotification(PushNotification $notification)
 {
     $app = $this->getSetting('App');
     $devices = array_filter(explode(',', $this->getSetting('Devices')), 'strlen');
     if (!$app) {
         throw new PushException('No application was selected.');
     }
     if (!isset(self::$applications[$app])) {
         throw new PushException(sprintf('No settings were provided for application "%s"', $app));
     }
     if (!$devices) {
         throw new PushException('At least one device type must be selected to send to.');
     }
     $user = self::$applications[$app]['key'];
     $pass = self::$applications[$app]['secret'];
     $request = function ($url, $payload) use($user, $pass) {
         $client = new Zend\Http\Client($url);
         $client->setAuth($user, $pass);
         $client->setHeaders('Content-Type: application/json');
         $client->setRawData(json_encode($payload), 'application/json');
         try {
             $response = $client->request('POST');
         } catch (Zend\Http\Client_Exception $e) {
             throw new PushException($e->getMessage(), $e->getCode(), $e);
         }
         if ($response->isError()) {
             throw new PushException($response->getBody(), $response->getStatus());
         }
     };
     // Use the V1 API for sending to Android, Blackberry and iOS.
     if (array_intersect($devices, array(self::ANDROID, self::BLACKBERRY, self::IOS))) {
         $body = array();
         if (in_array(self::ANDROID, $devices)) {
             $body['android'] = array('alert' => $notification->Content);
         }
         if (in_array(self::BLACKBERRY, $devices)) {
             $body['blackberry'] = array('content-type' => 'text/plain', 'body' => $notification->Content);
         }
         if (in_array(self::IOS, $devices)) {
             $body['aps'] = array('badge' => $this->getSetting('Badge') == 'inc' ? '+1' : $this->getSetting('Badge'), 'alert' => $notification->Content, 'sound' => $this->getSetting('Sound'));
         }
         $request(self::V1_API_URL . '/broadcast/', $body);
     }
     // Use the V2 API for sending to Windows.
     if (array_intersect($devices, array(self::MPNS, self::WNS))) {
         $types = array();
         if (in_array(self::MPNS, $devices)) {
             $types[] = 'mpns';
         }
         if (in_array(self::WNS, $devices)) {
             $types[] = 'wns';
         }
         $request(self::V2_API_URL . '/broadcast/', array('notification' => array('alert' => $notification->Content), 'device_types' => $types));
     }
 }
Example #2
0
 /**
  * Performs HTTP request to given $url using given HTTP $method.
  * Send additinal query specified by variable/value array,
  * On success returns HTTP response without headers, false on failure.
  *
  * @param string $url OpenID server url
  * @param string $method HTTP request method 'GET' or 'POST'
  * @param array $params additional qwery parameters to be passed with
  * @param int &$staus HTTP status code
  *  request
  * @return mixed
  */
 protected function _httpRequest($url, $method = 'GET', array $params = array(), &$status = null)
 {
     $client = $this->_httpClient;
     if ($client === null) {
         $client = new \Zend\Http\Client($url, array('maxredirects' => 4, 'timeout' => 15, 'useragent' => 'Zend_OpenId'));
     } else {
         $client->setUri($url);
     }
     $client->resetParameters();
     if ($method == 'POST') {
         $client->setMethod(\Zend\Http\Client::POST);
         $client->setParameterPost($params);
     } else {
         $client->setMethod(\Zend\Http\Client::GET);
         $client->setParameterGet($params);
     }
     try {
         $response = $client->request();
     } catch (\Exception $e) {
         $this->_setError('HTTP Request failed: ' . $e->getMessage());
         return false;
     }
     $status = $response->getStatus();
     $body = $response->getBody();
     if ($status == 200 || $status == 400 && !empty($body)) {
         return $body;
     } else {
         $this->_setError('Bad HTTP response');
         return false;
     }
 }
Example #3
0
/**
 * 执行GET操作
 *
 * @param string $url            
 * @param array $params            
 * @param boolean $returnObj            
 * @return string
 */
function doGet($url, $params = array(), $returnObj = false)
{
    try {
        $url = trim($url);
        if (!filter_var($url, FILTER_VALIDATE_URL)) {
            throw new Exception('Invalid URL');
            return false;
        }
        $client = new Zend\Http\Client();
        $client->setUri($url);
        $client->setMethod(Request::METHOD_GET);
        $client->setParameterGet($params);
        $client->setEncType(Zend\Http\Client::ENC_URLENCODED);
        $client->setOptions(array('maxredirects' => 5, 'strictredirects' => false, 'useragent' => 'Zend\\Http\\Client', 'timeout' => 10, 'adapter' => 'Zend\\Http\\Client\\Adapter\\Socket', 'httpversion' => Request::VERSION_11, 'storeresponse' => true, 'keepalive' => false, 'outputstream' => false, 'encodecookies' => true, 'argseparator' => null, 'rfc3986strict' => false));
        $response = $client->request('GET');
        return $returnObj ? $response : $response->getBody();
    } catch (Exception $e) {
        fb(exceptionMsg($e), \FirePHP::LOG);
        return false;
    }
}