本文整理汇总了PHP中GuzzleHttp\ClientInterface::post方法的典型用法代码示例。如果您正苦于以下问题:PHP ClientInterface::post方法的具体用法?PHP ClientInterface::post怎么用?PHP ClientInterface::post使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类GuzzleHttp\ClientInterface
的用法示例。
在下文中一共展示了ClientInterface::post方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: authenticate
/**
* Authenticates a user and returns an authentication token.
*
* @param string $username
* @param string $password
* @return TokenInterface
* @throws AccountDisabledException
* @throws AccountNotConfirmedException
* @throws AuthenticationException
* @throws AuthorizationLimitReachedException
* @throws UserNotFoundException
* @throws WrongPasswordException
*/
public function authenticate($username, $password)
{
$response = $this->httpClient->post('/auth', ['body' => ['username' => $username, 'password' => $password]])->json();
if (isset($response['error'])) {
switch ($response['code']) {
case 101:
throw new UserNotFoundException();
case 102:
throw new AccountNotConfirmedException();
case 103:
case 104:
case 105:
throw new AccountDisabledException();
case 106:
throw new AuthorizationLimitReachedException();
case 107:
throw new WrongPasswordException();
default:
throw new AuthenticationException('An error occurred during the authentication');
}
}
$token = new Token();
$token->setUid($response['uid']);
$token->setToken($response['token']);
return $token;
}
示例2: verify
/**
* @param VerifyYubikeyOtpCommand $command
* @return YubikeyVerificationResult
*/
public function verify(VerifyYubikeyOtpCommand $command)
{
$this->logger->info('Verifying Yubikey OTP');
$body = ['requester' => ['institution' => $command->institution, 'identity' => $command->identity], 'otp' => ['value' => $command->otp]];
$response = $this->guzzleClient->post('api/verify-yubikey', ['json' => $body, 'exceptions' => false]);
$statusCode = $response->getStatusCode();
if ($statusCode != 200) {
$type = $statusCode >= 400 && $statusCode < 500 ? 'client' : 'server';
$this->logger->info(sprintf('Yubikey OTP verification failed; %s error', $type));
return new YubikeyVerificationResult(true, false);
}
try {
$result = $response->json();
} catch (\RuntimeException $e) {
$this->logger->error('Yubikey OTP verification failed; server responded with malformed JSON.');
return new YubikeyVerificationResult(false, true);
}
if (!isset($result['status'])) {
$this->logger->error('Yubikey OTP verification failed; server responded without status report.');
return new YubikeyVerificationResult(false, true);
}
if ($result['status'] !== 'OK') {
$this->logger->error('Yubikey OTP verification failed; server responded with non-OK status report.');
return new YubikeyVerificationResult(false, true);
}
return new YubikeyVerificationResult(false, false);
}
示例3: send
/**
* {@inheritdoc}
*/
public function send(Swift_Mime_Message $message, &$failedRecipients = null)
{
$this->beforeSendPerformed($message);
$options = ['auth' => ['api', $this->key]];
$options['multipart'] = [['name' => 'to', 'contents' => $this->getTo($message)], ['name' => 'message', 'contents' => (string) $message, 'filename' => 'message.mime']];
return $this->client->post($this->url, $options);
}
示例4: sendSms
public function sendSms(SendSmsCommand $command)
{
$this->logger->info('Requesting Gateway to send SMS');
$body = ['requester' => ['institution' => $command->institution, 'identity' => $command->identity], 'message' => ['originator' => $command->originator, 'recipient' => $command->recipient, 'body' => $command->body]];
$response = $this->guzzleClient->post('api/send-sms', ['json' => $body, 'exceptions' => false]);
$statusCode = $response->getStatusCode();
if ($statusCode != 200) {
$this->logger->error(sprintf('SMS sending failed, error: [%s] %s', $response->getStatusCode(), $response->getReasonPhrase()), ['http-body' => $response->getBody() ? $response->getBody()->getContents() : '']);
return false;
}
try {
$result = $response->json();
} catch (\RuntimeException $e) {
$this->logger->error('SMS sending failed; server responded with malformed JSON.');
return false;
}
if (!isset($result['status'])) {
$this->logger->error('SMS sending failed; server responded without status report.');
return false;
}
if ($result['status'] !== 'OK') {
$this->logger->error('SMS sending failed; server responded with non-OK status report.');
return false;
}
return true;
}
示例5: notify
/**
* Notifies the Flowdock channel of a deployment stage being initiated
*
* @param string $branchName
* @param string $applicationName
* @param string $connectionName
* @param string $eventTitle
* @param string $threadTitle
* @return ResponseInterface
* @throws FlowdockApiException
*/
public function notify($branchName, $applicationName, $connectionName, $eventTitle, $threadTitle)
{
if (empty($branchName)) {
throw new \InvalidArgumentException('branchName is an Invalid Argument');
}
if (empty($applicationName)) {
throw new \InvalidArgumentException('applicationName is an Invalid Argument');
}
if (empty($connectionName)) {
throw new \InvalidArgumentException('connectionName is an Invalid Argument');
}
if (empty($eventTitle)) {
throw new \InvalidArgumentException('eventTitle is an Invalid Argument');
}
if (empty($threadTitle)) {
throw new \InvalidArgumentException('threadTitle is an Invalid Argument');
}
$title = $this->formatEventTitle($branchName, $applicationName, $connectionName, $eventTitle);
$body = json_encode(['flow_token' => $this->flowToken, 'event' => 'activity', 'author' => ['name' => get_current_user()], 'title' => $title, 'external_thread_id' => $this->externalThreadID, 'thread' => ['title' => $threadTitle, 'body' => '']]);
$clientOptions = ['headers' => ['Content-Type' => 'application/json'], 'body' => $body];
$response = $this->client->post(self::MESSAGE_API, $clientOptions);
if ($response->getStatusCode() != 202) {
throw new FlowdockApiException("Error: HTTP " . $response->getStatusCode() . " with message " . $response->getReasonPhrase());
}
return $response;
}
示例6: execute
/**
* Execute an operaction
*
* A successful operation will return result as an array.
* An unsuccessful operation will return null.
*
* @param OperationInterface $op
* @return array|null
*/
public function execute(OperationInterface $op)
{
try {
if ($op instanceof DeleteOperationInterface) {
$response = $this->httpClient->delete($op->getEndpoint(), ['headers' => $op->getHeaders()]);
} elseif ($op instanceof PostOperationInterface) {
$response = $this->httpClient->post($op->getEndpoint(), ['headers' => $op->getHeaders(), 'body' => $op->getData()]);
} elseif ($op instanceof PatchOperationInterface) {
$response = $this->httpClient->patch($op->getEndpoint(), ['headers' => $op->getHeaders(), 'body' => $op->getData()]);
} elseif ($op instanceof PutOperationInterface) {
$response = $this->httpClient->put($op->getEndpoint(), ['headers' => $op->getHeaders(), 'body' => $op->getData()]);
} else {
$response = $this->httpClient->get($op->getEndpoint());
}
} catch (GuzzleHttp\Exception\ClientException $e) {
throw new Exception\ClientException('ClientException occurred in request to Orchestrate: ' . $e->getMessage(), $e->getCode(), $e);
} catch (GuzzleHttp\Exception\ServerException $e) {
throw new Exception\ServerException('ServerException occurred in request to Orchestrate: ' . $e->getMessage(), $e->getCode(), $e);
}
$refLink = null;
$location = null;
if ($response->hasHeader('ETag')) {
$refLink = str_replace('"', '', $response->getHeaderLine('ETag'));
} elseif ($response->hasHeader('Link')) {
$refLink = str_replace(['<', '>; rel="next"'], ['', ''], $response->getHeaderLine('Link'));
}
if ($response->hasHeader('Location')) {
$location = $response->getHeaderLine('Location');
}
$rawValue = $response->getBody(true);
$value = json_decode($rawValue, true);
return $op->getObjectFromResponse($refLink, $location, $value, $rawValue);
}
示例7: post
/**
* {@inheritdoc}
*/
public function post($url, array $headers = array(), $content = '')
{
$options = array('headers' => $headers, 'body' => $content);
$request = $this->client->post($url, $options);
$this->response = $request;
return $this->response->getBody();
}
示例8: send
/**
* {@inheritdoc}
*/
public function send(Swift_Mime_Message $message, &$failedRecipients = null)
{
$this->beforeSendPerformed($message);
$data = ['key' => $this->key, 'to' => $this->getToAddresses($message), 'raw_message' => $message->toString(), 'async' => false];
$options = ['form_params' => $data];
$this->client->post('https://mandrillapp.com/api/1.0/messages/send-raw.json', $options);
return $this->numberOfRecipients($message);
}
示例9: send
/**
* {@inheritdoc}
*/
public function send(Swift_Mime_Message $message, &$failedRecipients = null)
{
$this->beforeSendPerformed($message);
$version = phpversion() ?? 'Unknown PHP version';
$os = PHP_OS ?? 'Unknown OS';
$this->client->post('https://api.postmarkapp.com/email', ['headers' => ['X-Postmark-Server-Token' => $this->serverToken, 'Content-Type' => 'application/json', 'User-Agent' => "postmark (PHP Version: {$version}, OS: {$os})"], 'json' => $this->getMessagePayload($message)]);
return $this->numberOfRecipients($message);
}
示例10: send
/**
* {@inheritdoc}
*/
public function send(Swift_Mime_Message $message, &$failedRecipients = null)
{
$this->beforeSendPerformed($message);
$recipients = $this->getRecipients($message);
$message->setBcc([]);
$options = ['headers' => ['Authorization' => $this->key], 'json' => ['recipients' => $recipients, 'content' => ['email_rfc822' => $message->toString()]]];
return $this->client->post('https://api.sparkpost.com/api/v1/transmissions', $options);
}
示例11: send
/**
* {@inheritdoc}
*/
public function send(Swift_Mime_Message $message, &$failedRecipients = null)
{
$this->beforeSendPerformed($message);
$recipients = $this->getRecipients($message);
$message->setBcc([]);
$options = ['headers' => ['Authorization' => $this->key], 'json' => ['recipients' => $recipients, 'content' => ['html' => $message->getBody(), 'from' => $this->getFrom($message), 'reply_to' => $this->getReplyTo($message), 'subject' => $message->getSubject()]]];
return $this->client->post('https://api.sparkpost.com/api/v1/transmissions', $options);
}
示例12: auth
/**
* Authentication
*/
public function auth()
{
$this->handlerStack->remove('reAuth');
$res = $this->client->post($this->config->get('authUri'), ['form_params' => ['client_id' => $this->config->get('clientId'), 'client_secret' => $this->config->get('clientSecret'), 'grant_type' => 'client_credentials', 'scope' => 'https://graph.microsoft.com/.default']]);
$json = \GuzzleHttp\json_decode($res->getBody(), true);
$now = new \DateTime('now', new \DateTimeZone('UTC'));
$json['expires_in'] = $now->getTimestamp() + $json['expires_in'];
$this->tokenStorage->write($json);
}
示例13: verifyIpnMessage
/**
* {@inheritdoc}
*/
public function verifyIpnMessage(Message $message)
{
$requestBody = array_merge(['cmd' => '_notify-validate'], $message->getAll());
try {
$response = $this->httpClient->post($this->serviceEndpoint, array('form_params' => $requestBody));
} catch (\Exception $e) {
throw new ServiceException($e->getMessage());
}
return new ServiceResponse((string) $response->getBody());
}
示例14: uuidApi
public function uuidApi($username)
{
$response = $this->client->post(static::UUID_API, array('headers' => array('Content-Type' => 'application/json'), 'body' => json_encode(array('agent' => 'minecraft', 'name' => $username))))->json(array('object' => true));
if (!$response) {
throw new \RuntimeException('Bad JSON from API: on username ' . $username);
} elseif (isset($response->error)) {
throw new \RuntimeException('Error from API: ' . $response->error . ' on username ' . $username);
}
return $response;
}
示例15: send
/**
* {@inheritdoc}
*/
public function send(Swift_Mime_Message $message, &$failedRecipients = null)
{
$options = ['auth' => ['api', $this->key]];
if (version_compare(ClientInterface::VERSION, '6') === 1) {
$options['multipart'] = [['name' => 'to', 'contents' => $this->getTo($message)], ['name' => 'message', 'contents' => (string) $message, 'filename' => 'message.mime']];
} else {
$options['body'] = ['to' => $this->getTo($message), 'message' => new PostFile('message', (string) $message)];
}
return $this->client->post($this->url, $options);
}