示例#1
2
 /**
  * @inheritdoc
  */
 public function send()
 {
     $body = ['aps' => []];
     if ($this->messageData !== null) {
         $body['aps'] = $this->messageData;
     }
     if ($this->messageText !== null) {
         $body['aps']['alert'] = $this->messageText;
         if (!isset($body['aps']['sound'])) {
             $body['aps']['sound'] = 'default';
         }
         if (!isset($body['aps']['badge'])) {
             $body['aps']['badge'] = 0;
         }
     }
     $bodyData = json_encode($body);
     $ok = [];
     $promises = [];
     foreach ($this->recipients as $recipient) {
         $url = sprintf('/3/device/%s', $recipient);
         $promises[] = $this->client->postAsync($url, ['body' => $bodyData])->then(function (ResponseInterface $response) use(&$ok, $recipient) {
             if ($response->getStatusCode() == 200) {
                 // Set to OK if we received a 200
                 $ok[] = $recipient;
             } else {
                 $this->failedRecipients[] = $recipient;
             }
         }, function () use($recipient) {
             $this->failedRecipients[] = $recipient;
         });
     }
     // Wait for all requests to complete
     Promise\unwrap($promises);
     return $ok;
 }
 /**
  * @inheritdoc
  */
 public function __invoke(Message $message, Deferred $deferred = null)
 {
     $messageData = $this->messageConverter->convertToArray($message);
     MessageDataAssertion::assert($messageData);
     $messageData['created_at'] = $message->createdAt()->format('Y-m-d\\TH:i:s.u');
     $promise = $this->guzzleClient->postAsync($this->uri, ['json' => $messageData]);
     if ($deferred) {
         if ($this->async) {
             $this->resolveOrRejectDeferredAsync($deferred, $promise);
         } else {
             $this->resolveOrRejectDeferredSync($deferred, $promise);
         }
     }
 }
 /**
  * @param string $uri
  * @param array  $body
  * @param array  $options
  *
  * @return Promise
  */
 public function postAsync($uri, array $body = [], array $options = [])
 {
     if (!empty($body)) {
         $options['json'] = $body;
     }
     return $this->guzzle->postAsync($uri, $options);
 }
示例#4
0
 static function slack()
 {
     return function ($log_entry, $options) {
         $client = new Client();
         $url = $options;
         if (is_array($options)) {
             if (!isset($options['url'])) {
                 throw new \LogicException('Slack requires either a string(url) or an array of options with the key url to be set to CURL');
             }
             $url = $options['url'];
         }
         $client->postAsync($url, array('body' => json_encode(array('text' => strtoupper($log_entry['level']['text']) . ': ' . $log_entry['msg']))));
     };
 }
示例#5
0
 /**
  * @inheritdoc
  */
 public function send()
 {
     $body = ['data' => []];
     if ($this->messageData !== null) {
         $body['data'] = $this->messageData;
     }
     if ($this->messageText !== null) {
         $body['data']['message'] = $this->messageText;
     }
     $ok = [];
     $promises = [];
     $recipients_chunked = array_chunk($this->recipients, 1000);
     foreach ($recipients_chunked as $recipients_part) {
         $body['registration_ids'] = $recipients_part;
         $promises[] = $this->client->postAsync('/gcm/send', ['body' => json_encode($body)])->then(function (ResponseInterface $response) use(&$ok, $recipients_part) {
             if ($response->getStatusCode() == 200) {
                 // Set to OK if we received a 200
                 $contents = json_decode($response->getBody()->getContents(), true);
                 $results = $contents['results'];
                 foreach ($recipients_part as $idx => $recipient) {
                     if (isset($results[$idx]['message_id']) && !isset($results[$idx]['error'])) {
                         $ok[] = $recipient;
                     } else {
                         $this->failedRecipients[] = $recipient;
                     }
                 }
             }
         }, function () use($recipients_part) {
             foreach ($recipients_part as $idx => $recipient) {
                 $this->failedRecipients[] = $recipient;
             }
         });
     }
     // Wait for all requests to complete
     Promise\unwrap($promises);
     return $ok;
 }
 /**
  * Call all webhooks asynchronous
  *
  * @param array $webhooks
  * @param       $eventData
  */
 private function callWebhooks($webhooks, $eventData)
 {
     foreach ($webhooks as $webhook) {
         $this->client->postAsync($webhook["url"], ["body" => json_encode($this->createRequestBody($eventData)), "verify" => false]);
     }
 }
require __DIR__ . '/vendor/autoload.php';
use GuzzleHttp\Client;
use GuzzleHttp\TransferStats;
use GuzzleHttp\HandlerStack;
$stats = function (TransferStats $stats) {
    echo $stats->getTransferTime() . " " . $stats->getEffectiveUri() . "\n";
};
// if you are going to use multiple clients, and want to make requests async among all of them
// you have to pass a handler into the client, so they all run in the same loop
$handler = HandlerStack::create();
$usernameClient = new Client(['base_uri' => 'http://localhost:8000/', 'timeout' => 10, 'on_stats' => $stats, 'handler' => $handler]);
$passwordClient = new Client(['base_uri' => 'http://localhost:8001/', 'timeout' => 10, 'on_stats' => $stats, 'handler' => $handler]);
$emailClient = new Client(['base_uri' => 'http://localhost:8002/', 'timeout' => 10, 'on_stats' => $stats, 'handler' => $handler]);
$start = microtime(true);
$username1 = $usernameClient->post('generate-username');
$username2 = $usernameClient->post('generate-username');
$password = $passwordClient->post('generate-password');
$email = $emailClient->post('generate-email');
$stop = microtime(true);
$time = $stop - $start;
echo "4 requests in {$time} seconds\n";
$start = microtime(true);
$promises = [];
$promises['username1'] = $usernameClient->postAsync('generate-username');
$promises['username2'] = $usernameClient->postAsync('generate-username');
$promises['password'] = $passwordClient->postAsync('generate-password');
$promises['email'] = $emailClient->postAsync('generate-email');
$results = GuzzleHttp\Promise\unwrap($promises);
$stop = microtime(true);
$time = $stop - $start;
echo "4 requests in {$time} seconds\n";