本文整理汇总了PHP中Guzzle\Http\ClientInterface::createRequest方法的典型用法代码示例。如果您正苦于以下问题:PHP ClientInterface::createRequest方法的具体用法?PHP ClientInterface::createRequest怎么用?PHP ClientInterface::createRequest使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Guzzle\Http\ClientInterface
的用法示例。
在下文中一共展示了ClientInterface::createRequest方法的13个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: createRequestForApiCall
private function createRequestForApiCall(ApiCallInterface $apiCall)
{
$request = $this->guzzleClient->createRequest($apiCall->getMethod(), $apiCall->getPath(), $apiCall->getHeaders(), $apiCall->getBody());
$query = $request->getQuery();
$query->merge($apiCall->getQuery());
$query->set('api_key', $this->apiKey);
return $request;
}
示例2: send
/**
* @param string $endpoint
* @param string $content
* @param array $headers
* @param array $files
*
* @return Response
*/
public function send($endpoint, $content, array $headers = array(), array $files = array())
{
$request = $this->client->createRequest(RequestInterface::POST, $endpoint, $headers, $content, array('exceptions' => false));
if ($files && $request instanceof EntityEnclosingRequestInterface) {
$request->addPostFiles($files);
}
$response = $request->send();
return new Response($response->getStatusCode(), $response->getBody(true));
}
示例3: download
public function download()
{
$progressFunctions = null;
if ($this->getProgressFunction()) {
$progressFunctions = $this->createProgressFunctions(array_fill(0, count($this->requests), 0));
}
$requests = array();
foreach ($this->requests as $i => $r) {
$requests[] = $request = $this->client->createRequest('GET', $r['url'], array(), null, array('save_to' => $r['file']));
if ($progressFunctions !== null) {
// Guzzle progress is too complex for my needs
$request->getCurlOptions()->add(CURLOPT_NOPROGRESS, false)->add(CURLOPT_PROGRESSFUNCTION, $progressFunctions[$i]);
}
}
$this->client->send($requests);
}
示例4: sendRequests
/**
* Sends all requests to each caching proxy server
*
* Requests are sent in parallel to minimise impact on performance.
*
* @param RequestInterface[] $requests Requests
*
* @throws ExceptionCollection
*/
private function sendRequests(array $requests)
{
$allRequests = array();
foreach ($requests as $request) {
$headers = $request->getHeaders()->toArray();
// Force to re-create Host header if empty, as Apache chokes on this. See #128 for discussion.
if (empty($headers['Host'])) {
unset( $headers['Host'] );
}
foreach ($this->servers as $server) {
$proxyRequest = $this->client->createRequest(
$request->getMethod(),
$server . $request->getResource(),
$headers
);
$allRequests[] = $proxyRequest;
}
}
try {
$this->client->send($allRequests);
} catch (GuzzleExceptionCollection $e) {
$this->handleException($e);
}
}
示例5: createRequest
/**
* Creates a request.
*
* @param \Ivory\HttpAdapter\Message\InternalRequestInterface $internalRequest The internal request.
* @param callable|null $success The success callable.
* @param callable|null $error The error callable.
*
* @return \Ivory\HttpAdapter\Message\RequestInterface The request.
*/
private function createRequest(InternalRequestInterface $internalRequest, $success = null, $error = null)
{
$request = $this->client->createRequest($internalRequest->getMethod(), (string) $internalRequest->getUri(), $this->prepareHeaders($internalRequest), $this->prepareContent($internalRequest), array('exceptions' => false, 'allow_redirects' => false, 'timeout' => $this->getConfiguration()->getTimeout(), 'connect_timeout' => $this->getConfiguration()->getTimeout()));
$request->setProtocolVersion($internalRequest->getProtocolVersion());
if (is_callable($success)) {
$messageFactory = $this->getConfiguration()->getMessageFactory();
$request->getEventDispatcher()->addListener('request.success', function (Event $event) use($messageFactory, $success, $internalRequest) {
$response = $messageFactory->createResponse($event['response']->getStatusCode(), $event['response']->getProtocolVersion(), $event['response']->getHeaders()->toArray(), BodyNormalizer::normalize(function () use($event) {
$resource = $event['response']->getBody()->getStream();
$event['response']->getBody()->detachStream();
return $resource;
}, $internalRequest->getMethod()));
$response = $response->withParameter('request', $internalRequest);
call_user_func($success, $response);
});
}
if (is_callable($error)) {
$httpAdapterName = $this->getName();
$request->getEventDispatcher()->addListener('request.exception', function (Event $event) use($error, $internalRequest, $httpAdapterName) {
$exception = HttpAdapterException::cannotFetchUri($event['exception']->getRequest()->getUrl(), $httpAdapterName, $event['exception']->getMessage());
$exception->setRequest($internalRequest);
call_user_func($error, $exception);
});
}
return $request;
}
示例6: prepareRequest
/**
* @param $method
* @param $fullUrl
* @param array $query
* @param string $body
* @param array $headers
* @param array $options
* @return RequestInterface
*/
public function prepareRequest($method, $fullUrl, array $query = array(), $body = '', array $headers = array(), array $options = array())
{
$query["sign"] = $this->getSignature($body);
$headers["Authorization"] = "TOKEN " . $this->apiToken;
$fullUrl = $this->formatUrl($fullUrl, $query);
$request = $this->client->createRequest($method, $fullUrl, $headers, $body, $options);
return $request;
}
示例7: getHttpStatus
/**
* @param $url
* @return int|null
*/
private function getHttpStatus($url)
{
$request = $this->httpClient->createRequest('GET', $url);
try {
return $request->send()->getStatusCode();
} catch (RequestException $e) {
}
return null;
}
示例8: createRequest
/**
* @param string $httpMethod
* @param string $path
*/
protected function createRequest($httpMethod, $path, $body = null, array $headers = array(), array $options = array())
{
$path = $this->options['api_version'] . '/' . $path;
if ($httpMethod === 'GET' && $body) {
$path .= false === strpos($path, '?') ? '?' : '&';
$path .= utf8_encode(http_build_query($body, '', '&'));
}
return $this->client->createRequest($httpMethod, $path, array_merge($this->headers, $headers), $body, $options);
}
示例9: send
/**
* get all actions
*
* @param ClientCommand $command
*
* @return Response|CommandResponse|DecryptedCommandResponse|StatusResponse
*/
public function send(ClientCommand $command)
{
$request = $this->client->createRequest($command->method(), $this->client->getBaseUrl() . $command->uri($this->cryptographyEngine), $this->headers($command->apiVersion(), $command->format(), $command->headers()), $command->body($this->cryptographyEngine), $this->config->getHttpClientConfig());
try {
$response = $request->send();
} catch (ClientErrorResponseException $exception) {
$response = $exception->getResponse();
} catch (ServerErrorResponseException $exception) {
$response = $exception->getResponse();
}
return $command->response($response, $this->cryptographyEngine);
}
示例10: request
/**
* @param string $method
* @param string $relativeUrl
* @param array $params
* @return array
* @throws Exception\BaseApiErrorResponseException
* @throws Exception\RuntimeException
* @throws Exception\RateLimitExceededException
*/
public function request($method, $relativeUrl, array $params = [])
{
if (!$this->token instanceof AccessToken) {
throw new RuntimeException('Not authorized yet.');
}
$request = $this->httpClient->createRequest($method, $relativeUrl, ['Authorization' => "Bearer {$this->token->accessToken}"], null, $params);
try {
$response = $this->httpClient->send($request);
$body = json_decode($response->getBody(), true) or [];
return $body;
} catch (BadResponseException $e) {
$body = json_decode($e->getResponse()->getBody(), true);
switch ($body['error_description']) {
case self::ACCESS_TOKEN_EXPIRED_MESSAGE:
$this->refresh();
return $this->request($method, $relativeUrl, $params);
case self::RATE_LIMIT_EXCEEDED_MESSAGE:
throw new RateLimitExceededException(self::RATE_LIMIT_EXCEEDED_MESSAGE);
default:
throw new BaseApiErrorResponseException($body, $e->getResponse()->getStatusCode());
}
}
}
示例11: sendRequests
/**
* Sends all requests to each caching proxy server.
*
* Requests are sent in parallel to minimise impact on performance.
*
* @param RequestInterface[] $requests Requests
*
* @throws ExceptionCollection
*/
private function sendRequests(array $requests)
{
$allRequests = [];
foreach ($requests as $request) {
/* @var RequestInterface $request */
$proxyRequest = $this->client->createRequest($request->getMethod(), $request->getUrl(), $request->getHeaders());
$allRequests[] = $proxyRequest;
}
try {
$this->client->send($allRequests);
} catch (MultiTransferException $e) {
$this->handleException($e);
}
}
示例12: createRequest
/**
* Create request
*
* @param string $method HTTP method
* @param string $url URL
* @param array $headers HTTP headers
*
* @return RequestInterface
*/
protected function createRequest($method, $url, array $headers = array())
{
return $this->client->createRequest($method, $url, $headers);
}
示例13: createRequest
/**
* Create a request object to send to the server
*
* @param string $httpMethod
* @param string $path
* @param mixed $body
* @param array $options
*
* @return \Guzzle\Http\Message\RequestInterface
*/
protected function createRequest($httpMethod, $path, $body = null, array $options = array())
{
return $this->client->createRequest($httpMethod, $path, $this->headers, $body, $options);
}