本文整理汇总了PHP中GuzzleHttp\Client::postAsync方法的典型用法代码示例。如果您正苦于以下问题:PHP Client::postAsync方法的具体用法?PHP Client::postAsync怎么用?PHP Client::postAsync使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类GuzzleHttp\Client
的用法示例。
在下文中一共展示了Client::postAsync方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: send
/**
* @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;
}
示例2: __invoke
/**
* @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);
}
}
}
示例3: postAsync
/**
* @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: slack
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: send
/**
* @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;
}
示例6: callWebhooks
/**
* 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]);
}
}
示例7: function
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";