本文整理汇总了PHP中Psr\Http\Message\RequestInterface::withHeader方法的典型用法代码示例。如果您正苦于以下问题:PHP RequestInterface::withHeader方法的具体用法?PHP RequestInterface::withHeader怎么用?PHP RequestInterface::withHeader使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Psr\Http\Message\RequestInterface
的用法示例。
在下文中一共展示了RequestInterface::withHeader方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: fetchResumeUri
private function fetchResumeUri()
{
$result = null;
$body = $this->request->getBody();
if ($body) {
$headers = array('content-type' => 'application/json; charset=UTF-8', 'content-length' => $body->getSize(), 'x-upload-content-type' => $this->mimeType, 'x-upload-content-length' => $this->size, 'expect' => '');
foreach ($headers as $key => $value) {
$this->request = $this->request->withHeader($key, $value);
}
}
$response = $this->client->execute($this->request, false);
$location = $response->getHeaderLine('location');
$code = $response->getStatusCode();
if (200 == $code && true == $location) {
return $location;
}
$message = $code;
$body = json_decode((string) $this->request->getBody(), true);
if (isset($body['error']['errors'])) {
$message .= ': ';
foreach ($body['error']['errors'] as $error) {
$message .= "{$error[domain]}, {$error[message]};";
}
$message = rtrim($message, ';');
}
$error = "Failed to start the resumable upload (HTTP {$message})";
$this->client->getLogger()->error($error);
throw new GoogleException($error);
}
示例2: sign
/**
* @param \Psr\Http\Message\RequestInterface $request
*
* @return \Psr\Http\Message\RequestInterface
*/
public function sign(RequestInterface $request)
{
$timestamp = (new \DateTime('now', new \DateTimeZone('UTC')))->getTimestamp();
$data = implode('|', [$request->getMethod(), rtrim((string) $request->getUri(), '/'), $timestamp]);
$signature = $this->signer->sign($data);
return $request->withHeader(self::TIMESTAMP_HEADER, $timestamp)->withHeader(self::SIGNATURE_HEADER, $signature);
}
示例3: authenticate
/**
* {@inheritdoc}
*/
public function authenticate(RequestInterface $request)
{
// TODO: add error handling
// TODO: support other grant types?
$accessToken = $this->provider->getAccessToken('client_credentials');
return $request->withHeader('Authorization', 'Bearer ' . $accessToken);
}
示例4: authenticate
/**
* {@inheritdoc}
*/
public function authenticate(RequestInterface $request)
{
// TODO: generate better nonce?
$nonce = substr(md5(uniqid(uniqid() . '_', true)), 0, 16);
$created = date('c');
$digest = base64_encode(sha1(base64_decode($nonce) . $created . $this->password, true));
$wsse = sprintf('UsernameToken Username="%s", PasswordDigest="%s", Nonce="%s", Created="%s"', $this->username, $digest, $nonce, $created);
return $request->withHeader('Authorization', 'WSSE profile="UsernameToken"')->withHeader('X-WSSE', $wsse);
}
示例5: onRequest
/**
*
* @param RequestInterface $request
*/
public function onRequest(RequestInterface $request)
{
reset($this->headers);
$newRequest = $request->withHeader('Host', $request->getUri()->getHost())->withHeader('User-Agent', self::USER_AGENT);
$addHeaders = function (&$array, RequestInterface $request, callable $callback) {
$item = each($array);
if (!$item) {
return $request;
}
return $callback($array, $request, $callback)->withHeader($item['key'], $item['value']);
};
return $addHeaders($this->headers, $newRequest, $addHeaders);
}
示例6: __invoke
/**
* @param RequestInterface $request
*
* @return RequestInterface
*/
public function __invoke(RequestInterface $request)
{
$uri = $request->getUri();
$path = $uri->getPath();
$path .= $uri->getQuery() != null ? '?' . $uri->getQuery() : '';
$payload = ['key' => 'master', 'exp' => time() + $this->exp, 'method' => $request->getMethod(), 'path' => $path];
if (in_array($request->getMethod(), ['PUT', 'POST'])) {
$body = $request->getBody();
$computedHash = \GuzzleHttp\Psr7\hash($body, 'sha256');
$payload['body'] = ['alg' => 'sha256', 'hash' => $computedHash];
}
$jws = new JWS(['typ' => 'JWT', 'alg' => 'HS256']);
$jws->setPayload($payload)->sign($this->secret);
$token = $jws->getTokenString();
return $request->withHeader('Authorization', 'JWT token="' . $token . '"');
}
示例7: addHeaderToRequest
/**
* @param string $header
* @param string|string[] $value
*/
public function addHeaderToRequest($header, $value)
{
$this->request = $this->request->withHeader($header, $value);
}
示例8: withHeader
public function withHeader($name, $value)
{
$new = clone $this;
$new->request = $this->request->withHeader($name, $value);
return $new;
}
示例9: __construct
/**
* @since 2.0.0
* @param string $dsn Data Source Name
*/
public function __construct($dsn)
{
$this->driver = new CurlDriver();
$this->request = new Request($dsn);
$this->request->withHeader('Content-Type', 'application/json');
}
示例10: applyRequest
/**
* {@inheritdoc}
*/
public function applyRequest(HttpRequestInterface $request, array $options)
{
return $request->withHeader('Accept-Encoding', 'gzip;q=1.0,deflate;q=0.6,identity;q=0.3')->withHeader('Content-Type', 'application/json');
}
示例11: handleRequest
/**
* Add authentication data to the request and sends it. Calls handleResponse when done.
*
* @param RequestInterface $request
*
* @return mixed
*/
protected function handleRequest(RequestInterface $request, array $requestOptions = [])
{
$request->withHeader('Authorization', 'Bearer ' . $this->options['token']);
try {
$response = $this->httpClient->send($request);
} catch (ClientException $ex) {
return $this->handleResponse($ex->getResponse());
}
return $this->handleResponse($response, $requestOptions);
}
示例12: withHeader
/**
* @param string $header
* @param string|\string[] $value
* @return \Psr\Http\Message\MessageInterface
*/
public function withHeader($header, $value)
{
$this->request = $this->request->withHeader($header, $value);
return $this;
}
示例13: authenticate
/**
* {@inheritdoc}
*/
public function authenticate(RequestInterface $request)
{
$header = sprintf('Basic %s', base64_encode(sprintf('%s:%s', $this->username, $this->password)));
return $request->withHeader('Authorization', $header);
}
示例14: __invoke
/**
* Add HTTP header to request.
*
* {@inheritdoc}
*/
public function __invoke(Request $request, Response $response, callable $next)
{
return call_user_func($next, $request->withHeader(self::HEADER, $this->key), $response);
}
示例15: __invoke
public function __invoke(RequestInterface $request, array $options)
{
$fn = $this->nextHandler;
if (!isset($options['oxygen_site']) || !$options['oxygen_site'] instanceof Site) {
throw new \RuntimeException(sprintf('The option "oxygen_site" is expected to contain an instance of %s.', Site::class));
}
if (!isset($options['oxygen_action']) || !$options['oxygen_action'] instanceof ActionInterface) {
throw new \RuntimeException(sprintf('The option "oxygen_action" is expected to contain an instance of %s.', ActionInterface::class));
}
$transferInfo = $previousCallable = null;
if (isset($options['on_stats'])) {
$previousCallable = $options['on_stats'];
}
$options['on_stats'] = function (TransferStats $stats) use(&$transferInfo, $previousCallable) {
$transferInfo = new TransferInfo($stats->getHandlerStats());
if ($previousCallable) {
/* @var callable $previousCallable */
$previousCallable($stats);
}
};
/** @var Site $site */
$site = $options['oxygen_site'];
/** @var ActionInterface $action */
$action = $options['oxygen_action'];
$options['request_id'] = $requestId = \Undine\Functions\generate_uuid();
// Kind of like str_rot8, for hexadecimal strings.
$responseId = strtr($requestId, 'abcdef0123456789', '23456789abcdef01');
$expiresAt = time() + 86400;
$userName = '';
$stateParameters = $this->stateTracker->getParameters($site);
$requestData = ['oxygenRequestId' => $requestId, 'requestExpiresAt' => $expiresAt, 'publicKey' => $site->getPublicKey(), 'signature' => \Undine\Functions\openssl_sign_data($site->getPrivateKey(), sprintf('%s|%d', $requestId, $expiresAt)), 'handshakeKey' => $this->handshakeKeyName, 'handshakeSignature' => \Undine\Functions\openssl_sign_data($this->handshakeKeyValue, $this->getUrlSlug($site->getUrl())), 'version' => $this->moduleVersion, 'baseUrl' => (string) $site->getUrl(), 'actionName' => $action->getName(), 'actionParameters' => $action->getParameters(), 'userName' => $userName, 'userId' => $site->getUser()->getId(), 'stateParameters' => $stateParameters];
$oxygenRequest = $request->withHeader('accept', 'text/html,application/json,application/oxygen')->withBody(\GuzzleHttp\Psr7\stream_for(json_encode($requestData)));
if ($site->hasHttpCredentials()) {
$oxygenRequest = $oxygenRequest->withHeader('Authorization', 'Basic ' . base64_encode(sprintf('%s:%s', $site->getHttpCredentials()->getUsername(), $site->getHttpCredentials()->getPassword())));
}
return $fn($oxygenRequest, $options)->then(function (ResponseInterface $response) use($site, $request, $options, &$transferInfo, $responseId, $action) {
$responseData = $this->extractData($responseId, $request, $options, $response, $transferInfo);
try {
$reaction = $this->createReaction($action, $responseData);
} catch (ExceptionInterface $e) {
throw new ResponseException(ResponseException::ACTION_RESULT_MALFORMED, $request, $options, $response, $transferInfo, $e);
}
try {
$this->stateTracker->setResult($site, $responseData['stateResult']);
} catch (\Exception $e) {
throw new ResponseException(ResponseException::STATE_MALFORMED, $request, $options, $response, $transferInfo, $e);
}
return $reaction;
}, function (RequestException $e) use($options, &$transferInfo) {
throw new NetworkException($e->getHandlerContext()['errno'], $e->getRequest(), $options, $e->getResponse(), $transferInfo);
})->otherwise(function (\Exception $exception) use($site) {
$this->stateTracker->setException($site, $exception);
throw $exception;
});
}