本文整理汇总了PHP中GuzzleHttp\Psr7\modify_request函数的典型用法代码示例。如果您正苦于以下问题:PHP modify_request函数的具体用法?PHP modify_request怎么用?PHP modify_request使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了modify_request函数的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: __invoke
/**
* @param RequestInterface $request
* @param array $options
*
* @return PromiseInterface
*/
public function __invoke(RequestInterface $request, array $options)
{
$fn = $this->nextHandler;
// Don't do anything if the request has no body.
if (isset(self::$skipMethods[$request->getMethod()]) || $request->getBody()->getSize() === 0) {
return $fn($request, $options);
}
$modify = [];
// Add a default content-type if possible.
if (!$request->hasHeader('Content-Type')) {
if ($uri = $request->getBody()->getMetadata('uri')) {
if ($type = Psr7\mimetype_from_filename($uri)) {
$modify['set_headers']['Content-Type'] = $type;
}
}
}
// Add a default content-length or transfer-encoding header.
if (!isset(self::$skipMethods[$request->getMethod()]) && !$request->hasHeader('Content-Length') && !$request->hasHeader('Transfer-Encoding')) {
$size = $request->getBody()->getSize();
if ($size !== null) {
$modify['set_headers']['Content-Length'] = $size;
} else {
$modify['set_headers']['Transfer-Encoding'] = 'chunked';
}
}
// Add the expect header if needed.
$this->addExpectHeader($request, $options, $modify);
return $fn(Psr7\modify_request($request, $modify), $options);
}
示例2: __invoke
/**
* This method is invoked before every HTTP request is sent to the API. When this happens, it
* checks to see whether a token is set and valid, and then sets the ``X-Auth-Token`` header
* for the HTTP request before letting it continue on its merry way.
*
* @param RequestInterface $request
* @param array $options
*
* @return mixed|void
*/
public function __invoke(RequestInterface $request, array $options)
{
$fn = $this->nextHandler;
if ($this->shouldIgnore($request)) {
return $fn($request, $options);
}
if (!$this->token || $this->token->hasExpired()) {
$this->token = call_user_func($this->tokenGenerator);
}
$modify = ['set_headers' => ['X-Auth-Token' => $this->token->getId()]];
return $fn(modify_request($request, $modify), $options);
}
示例3: returnHandlers
/**
* Set the middleware handlers for all requests using Oauth2
*
* @return HandlerStack|null
*/
protected function returnHandlers()
{
// Create a handler stack that has all of the default middlewares attached
$handler = HandlerStack::create();
//Add the Authorization header to requests.
$handler->push(Middleware::mapRequest(function (RequestInterface $request) {
if ($this->getConfig('auth') == 'oauth2') {
$token = $this->getAccessToken();
if ($token !== null) {
$request = $request->withHeader('Authorization', 'Bearer ' . $token->getToken());
return $request;
}
}
return $request;
}), 'add_oauth_header');
$handler->before('add_oauth_header', $this->retry_modify_request(function ($retries, RequestInterface $request, ResponseInterface $response = null, $error = null) {
if ($retries > 0) {
return false;
}
if ($response instanceof ResponseInterface) {
if ($response->getStatusCode() == 401) {
return true;
}
}
return false;
}, function (RequestInterface $request, ResponseInterface $response) {
if ($response instanceof ResponseInterface) {
if ($response->getStatusCode() == 401) {
$token = $this->acquireAccessToken();
$this->setAccessToken($token, 'Bearer');
$modify['set_headers']['Authorization'] = 'Bearer ' . $token->getToken();
return Psr7\modify_request($request, $modify);
}
}
return $request;
}));
return $handler;
}
示例4: modifyRequest
public function modifyRequest()
{
return $this->retry_modify_request(function ($retries, RequestInterface $request, ResponseInterface $response = null, $error = null) {
if ($retries > 0) {
return false;
}
if ($response instanceof ResponseInterface) {
if (in_array($response->getStatusCode(), [400, 401])) {
return true;
}
}
return false;
}, function (RequestInterface $request, ResponseInterface $response) {
if ($response instanceof ResponseInterface) {
if (in_array($response->getStatusCode(), [400, 401])) {
$token = $this->acquireAccessToken();
$this->setAccessToken($token, 'Bearer');
$modify['set_headers']['X-SFDC-Session'] = $token->getToken();
return Psr7\modify_request($request, $modify);
}
}
return $request;
});
}
示例5: applyOptions
/**
* Applies the array of request options to a request.
*
* @param RequestInterface $request
* @param array $options
*
* @return RequestInterface
*/
private function applyOptions(RequestInterface $request, array &$options)
{
$modify = [];
if (isset($options['form_params'])) {
if (isset($options['multipart'])) {
throw new \InvalidArgumentException('You cannot use ' . 'form_params and multipart at the same time. Use the ' . 'form_params option if you want to send application/' . 'x-www-form-urlencoded requests, and the multipart ' . 'option to send multipart/form-data requests.');
}
$options['body'] = http_build_query($options['form_params'], '', '&');
unset($options['form_params']);
$options['_conditional']['Content-Type'] = 'application/x-www-form-urlencoded';
}
if (isset($options['multipart'])) {
$options['body'] = new Psr7\MultipartStream($options['multipart']);
unset($options['multipart']);
}
if (isset($options['json'])) {
$options['body'] = \GuzzleHttp\json_encode($options['json']);
unset($options['json']);
$options['_conditional']['Content-Type'] = 'application/json';
}
if (!empty($options['decode_content']) && $options['decode_content'] !== true) {
$modify['set_headers']['Accept-Encoding'] = $options['decode_content'];
}
if (isset($options['headers'])) {
if (isset($modify['set_headers'])) {
$modify['set_headers'] = $options['headers'] + $modify['set_headers'];
} else {
$modify['set_headers'] = $options['headers'];
}
unset($options['headers']);
}
if (isset($options['body'])) {
if (is_array($options['body'])) {
$this->invalidBody();
}
$modify['body'] = Psr7\stream_for($options['body']);
unset($options['body']);
}
if (!empty($options['auth']) && is_array($options['auth'])) {
$value = $options['auth'];
$type = isset($value[2]) ? strtolower($value[2]) : 'basic';
switch ($type) {
case 'basic':
$modify['set_headers']['Authorization'] = 'Basic ' . base64_encode("{$value['0']}:{$value['1']}");
break;
case 'digest':
// @todo: Do not rely on curl
$options['curl'][CURLOPT_HTTPAUTH] = CURLAUTH_DIGEST;
$options['curl'][CURLOPT_USERPWD] = "{$value['0']}:{$value['1']}";
break;
}
}
if (isset($options['query'])) {
$value = $options['query'];
if (is_array($value)) {
$value = http_build_query($value, null, '&', PHP_QUERY_RFC3986);
}
if (!is_string($value)) {
throw new \InvalidArgumentException('query must be a string or array');
}
$modify['query'] = $value;
unset($options['query']);
}
// Ensure that sink is not an invalid value.
if (isset($options['sink'])) {
// TODO: Add more sink validation?
if (is_bool($options['sink'])) {
throw new \InvalidArgumentException('sink must not be a boolean');
}
}
$request = Psr7\modify_request($request, $modify);
if ($request->getBody() instanceof Psr7\MultipartStream) {
// Use a multipart/form-data POST if a Content-Type is not set.
$options['_conditional']['Content-Type'] = 'multipart/form-data; boundary=' . $request->getBody()->getBoundary();
}
// Merge in conditional headers if they are not present.
if (isset($options['_conditional'])) {
// Build up the changes so it's in a single clone of the message.
$modify = [];
foreach ($options['_conditional'] as $k => $v) {
if (!$request->hasHeader($k)) {
$modify['set_headers'][$k] = $v;
}
}
$request = Psr7\modify_request($request, $modify);
// Don't pass this internal value along to middleware/handlers.
unset($options['_conditional']);
}
return $request;
}
示例6: testAddsQueryToUri
public function testAddsQueryToUri()
{
$r1 = new Psr7\Request('GET', 'http://foo.com');
$r2 = Psr7\modify_request($r1, ['query' => 'foo=bar']);
$this->assertNotSame($r1, $r2);
$this->assertEquals('foo=bar', $r2->getUri()->getQuery());
}
示例7: createRequestFromFollow
private function createRequestFromFollow(Step $step, ValueBag $values, RequestInterface $request = null, ResponseInterface $response = null, Crawler $crawler = null)
{
if (null === $request || null === $response) {
throw new CrawlException('Unable to follow when no previous page.');
}
if ('3' !== substr($response->getStatusCode(), 0, 1) || !$response->hasHeader('Location')) {
throw new CrawlException('Unable to follow when no previous page is not a redirect.');
}
// logic from Guzzle\RedirectMiddleware
// Request modifications to apply.
$modify = [];
// Use a GET request if this is an entity enclosing request and we are
// not forcing RFC compliance, but rather emulating what all browsers
// would do.
$statusCode = $response->getStatusCode();
if ($statusCode == 303 || $statusCode <= 302 && $request->getBody()) {
$modify['method'] = 'GET';
$modify['body'] = '';
}
$modify['uri'] = Psr7\Uri::resolve($request->getUri(), $response->getHeaderLine('Location'));
Psr7\rewind_body($request);
// Add the Referer header only if we are not redirecting from https to http
if ($modify['uri']->getScheme() === $request->getUri()->getScheme()) {
$modify['set_headers']['Referer'] = (string) $request->getUri()->withUserInfo('', '');
} else {
$modify['remove_headers'][] = 'Referer';
}
// Remove Authorization header if host is different
if ($request->getUri()->getHost() !== $modify['uri']->getHost()) {
$modify['remove_headers'][] = 'Authorization';
}
return Psr7\modify_request($request, $modify);
}
示例8: testModifyRequestKeepInstanceOfRequest
public function testModifyRequestKeepInstanceOfRequest()
{
$r1 = new Psr7\Request('GET', 'http://foo.com');
$r2 = Psr7\modify_request($r1, ['remove_headers' => ['non-existent']]);
$this->assertTrue($r2 instanceof Psr7\Request);
$r1 = new Psr7\ServerRequest('GET', 'http://foo.com');
$r2 = Psr7\modify_request($r1, ['remove_headers' => ['non-existent']]);
$this->assertTrue($r2 instanceof ServerRequestInterface);
}
示例9: signRequest
/**
* Sign the request.
*
* @param RequestInterface $request Psr7 request.
* @return RequestInterface
*/
private function signRequest(RequestInterface $request)
{
$headers = ['User-Agent' => 'gcloud-php/' . ServiceBuilder::VERSION, 'Authorization' => 'Bearer ' . $this->getToken()];
return Psr7\modify_request($request, ['set_headers' => $headers]);
}
示例10: applyOptions
/**
* Applies the array of request options to a request.
*
* @param RequestInterface $request
* @param array $options
*
* @return RequestInterface
*/
private function applyOptions(RequestInterface $request, array &$options)
{
$modify = [];
if (isset($options['form_params'])) {
$options['body'] = http_build_query($options['form_params']);
unset($options['form_params']);
$options['_conditional']['Content-Type'] = 'application/x-www-form-urlencoded';
}
if (isset($options['multipart'])) {
$elements = $options['multipart'];
unset($options['multipart']);
$options['body'] = new Psr7\MultipartStream($elements);
// Use a multipart/form-data POST if a Content-Type is not set.
$options['_conditional']['Content-Type'] = 'multipart/form-data; boundary=' . $options['body']->getBoundary();
}
if (!empty($options['decode_content']) && $options['decode_content'] !== true) {
$modify['set_headers']['Accept-Encoding'] = $options['decode_content'];
}
if (isset($options['headers'])) {
if (isset($modify['set_headers'])) {
$modify['set_headers'] = $options['headers'] + $modify['set_headers'];
} else {
$modify['set_headers'] = $options['headers'];
}
unset($options['headers']);
}
if (isset($options['body'])) {
if (is_array($options['body'])) {
$this->invalidBody();
}
$modify['body'] = Psr7\stream_for($options['body']);
unset($options['body']);
}
if (!empty($options['auth'])) {
$value = $options['auth'];
$type = is_array($value) ? isset($value[2]) ? strtolower($value[2]) : 'basic' : $value;
$config['auth'] = $value;
switch (strtolower($type)) {
case 'basic':
$modify['set_headers']['Authorization'] = 'Basic ' . base64_encode("{$value['0']}:{$value['1']}");
break;
case 'digest':
// @todo: Do not rely on curl
$options['curl'][CURLOPT_HTTPAUTH] = CURLAUTH_DIGEST;
$options['curl'][CURLOPT_USERPWD] = "{$value['0']}:{$value['1']}";
break;
}
}
if (isset($options['query'])) {
$value = $options['query'];
if (is_array($value)) {
$value = http_build_query($value, null, '&', PHP_QUERY_RFC3986);
}
if (!is_string($value)) {
throw new Iae('query must be a string or array');
}
$modify['query'] = $value;
unset($options['query']);
}
if (isset($options['json'])) {
$modify['body'] = Psr7\stream_for(json_encode($options['json']));
$options['_conditional']['Content-Type'] = 'application/json';
unset($options['json']);
}
$request = Psr7\modify_request($request, $modify);
// Merge in conditional headers if they are not present.
if (isset($options['_conditional'])) {
// Build up the changes so it's in a single clone of the message.
$modify = [];
foreach ($options['_conditional'] as $k => $v) {
if (!$request->hasHeader($k)) {
$modify['set_headers'][$k] = $v;
}
}
$request = Psr7\modify_request($request, $modify);
// Don't pass this internal value along to middleware/handlers.
unset($options['_conditional']);
}
return $request;
}
示例11: modifyRequest
/**
* @param RequestInterface $request
* @param array $options
* @param ResponseInterface $response
*
* @return RequestInterface
*/
public function modifyRequest(RequestInterface $request, array $options, ResponseInterface $response)
{
// Request modifications to apply.
$modify = [];
$protocols = $options['allow_redirects']['protocols'];
// Use a GET request if this is an entity enclosing request and we are
// not forcing RFC compliance, but rather emulating what all browsers
// would do.
$statusCode = $response->getStatusCode();
if ($statusCode == 303 || $statusCode <= 302 && $request->getBody() && !$options['allow_redirects']['strict']) {
$modify['method'] = 'GET';
$modify['body'] = '';
}
$modify['uri'] = $this->redirectUri($request, $response, $protocols);
Psr7\rewind_body($request);
// Add the Referer header if it is told to do so and only
// add the header if we are not redirecting from https to http.
if ($options['allow_redirects']['referer'] && $modify['uri']->getScheme() === $request->getUri()->getScheme()) {
$uri = $request->getUri()->withUserInfo('', '');
$modify['set_headers']['Referer'] = (string) $uri;
} else {
$modify['remove_headers'][] = 'Referer';
}
return Psr7\modify_request($request, $modify);
}
示例12: deleteFiles
/**
* Concurrently delete the files.
*
* @param array $files
* Array of file ID's.
* @param OutputInterface $output
* Console output interface.
*/
protected function deleteFiles(array $files, OutputInterface $output)
{
$requests = function ($files) {
$uri = "https://slack.com/api/files.delete";
foreach ($files as $file_id) {
$req = new Request('POST', $uri);
$modify['body'] = Psr7\stream_for(http_build_query(['file' => $file_id, 'token' => getenv('SLACK_AUTH_TOKEN')]));
$modify['set_headers']['Content-Type'] = 'application/x-www-form-urlencoded';
$modify['set_headers']['Cache-Control'] = 'no-cache';
$request = Psr7\modify_request($req, $modify);
(yield $request);
}
};
$pool = new Pool($this->client, $requests($files), ['concurrency' => self::CONCURRENT_REQUESTS, 'fulfilled' => function ($response, $index) use($output) {
$output->writeln("<info>File deleted</info>");
}, 'rejected' => function ($reason, $index) use($output) {
$output->writeln("<error>Error deleting file {$reason}</error>");
}]);
$promise = $pool->promise();
$promise->wait();
}