本文整理汇总了PHP中GuzzleHttp\ClientInterface::createRequest方法的典型用法代码示例。如果您正苦于以下问题:PHP ClientInterface::createRequest方法的具体用法?PHP ClientInterface::createRequest怎么用?PHP ClientInterface::createRequest使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类GuzzleHttp\ClientInterface
的用法示例。
在下文中一共展示了ClientInterface::createRequest方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: sendPostRequest
/**
* @param string $url
* @param mixed[]|null $body
* @return \SlevomatZboziApi\Response\ZboziApiResponse
*/
public function sendPostRequest($url, array $body = null)
{
TypeValidator::checkString($url);
$options = ['allow_redirects' => false, 'verify' => true, 'decode_content' => true, 'expect' => false, 'timeout' => $this->timeoutInSeconds];
$request = $this->client->createRequest('POST', $url, $options);
$request->setHeaders([static::HEADER_PARTNER_TOKEN => $this->partnerToken, static::HEADER_API_SECRET => $this->apiSecret]);
if ($body !== null) {
$request->setBody(\GuzzleHttp\Stream\Stream::factory(json_encode($body)));
}
try {
try {
$response = $this->client->send($request);
$this->log($request, $response);
return $this->getZboziApiResponse($response);
} catch (\GuzzleHttp\Exception\RequestException $e) {
$response = $e->getResponse();
$this->log($request, $response);
if ($response !== null) {
return $this->getZboziApiResponse($response);
}
throw new \SlevomatZboziApi\Request\ConnectionErrorException('Connection to Slevomat API failed.', $e->getCode(), $e);
}
} catch (\GuzzleHttp\Exception\ParseException $e) {
$this->log($request, isset($response) ? $response : null, true);
throw new \SlevomatZboziApi\Response\ResponseErrorException('Slevomat API invalid response: invalid JSON data.', $e->getCode(), $e);
}
}
示例2: callApi
public function callApi($call, $method = 'GET', array $options = [])
{
$time = microtime(true);
$request = $this->_guzzle->createRequest($method, $this->_baseUri . '/' . $call, $options);
if ($this->_headers) {
foreach ($this->_headers as $header => $value) {
$request->addHeader($header, $value);
}
}
if (!$this->isBatchOpen()) {
try {
return $this->_processResponse($this->_guzzle->send($request), $time);
} catch (RequestException $e) {
$response = $e->getResponse();
if ($response) {
return $this->_processResponse($response, $time);
} else {
throw $e;
}
}
}
$batchId = uniqid($method, true);
$request->addHeader('X-Batch-ID', $batchId);
$apiResult = new ApiResult();
$this->_batch[] = $request;
$this->_results[$batchId] = $apiResult;
return $apiResult;
}
示例3: testOnlyResponse
public function testOnlyResponse()
{
$request = $this->guzzleHttpClient->createRequest('GET', 'http://petstore.swagger.io/v2/pet/findByStatus');
$request->addHeader('Accept', 'application/json');
$response = $this->guzzleHttpClient->send($request);
$this->assertResponseMatch($response, self::$schemaManager, '/v2/pet/findByStatus', 'get');
}
示例4: fetch
/**
* {@inheritdoc}
*/
public function fetch(FeedInterface $feed)
{
$request = $this->httpClient->createRequest('GET', $feed->getUrl());
$feed->source_string = FALSE;
// Generate conditional GET headers.
if ($feed->getEtag()) {
$request->addHeader('If-None-Match', $feed->getEtag());
}
if ($feed->getLastModified()) {
$request->addHeader('If-Modified-Since', gmdate(DateTimePlus::RFC7231, $feed->getLastModified()));
}
try {
$response = $this->httpClient->send($request);
// In case of a 304 Not Modified, there is no new content, so return
// FALSE.
if ($response->getStatusCode() == 304) {
return FALSE;
}
$feed->source_string = $response->getBody(TRUE);
$feed->setEtag($response->getHeader('ETag'));
$feed->setLastModified(strtotime($response->getHeader('Last-Modified')));
$feed->http_headers = $response->getHeaders();
// Update the feed URL in case of a 301 redirect.
if ($response->getEffectiveUrl() != $feed->getUrl()) {
$feed->setUrl($response->getEffectiveUrl());
}
return TRUE;
} catch (RequestException $e) {
$this->logger->warning('The feed from %site seems to be broken because of error "%error".', array('%site' => $feed->label(), '%error' => $e->getMessage()));
drupal_set_message(t('The feed from %site seems to be broken because of error "%error".', array('%site' => $feed->label(), '%error' => $e->getMessage())), 'warning');
return FALSE;
}
}
示例5: testFetchPetBodyMatchDefinition
public function testFetchPetBodyMatchDefinition()
{
$request = $this->guzzleHttpClient->createRequest('GET', 'http://petstore.swagger.io/v2/pet/findByStatus');
$request->addHeader('Accept', 'application/json');
$response = $this->guzzleHttpClient->send($request);
$responseBody = $response->json(['object' => true]);
$this->assertResponseBodyMatch($responseBody, self::$schemaManager, '/v2/pet/findByStatus', 'get', 200);
}
示例6: createRequest
/**
* Converts a PSR request into a Guzzle request.
*
* @param RequestInterface $request
*
* @return GuzzleRequest
*/
private function createRequest(RequestInterface $request)
{
$options = ['exceptions' => false, 'allow_redirects' => false];
$options['version'] = $request->getProtocolVersion();
$options['headers'] = $request->getHeaders();
$options['body'] = (string) $request->getBody();
return $this->client->createRequest($request->getMethod(), (string) $request->getUri(), $options);
}
示例7: sendRequest
/**
* @param string $method
* @param string $path
* @param array $options
*
* @return array
*/
public function sendRequest($method, $path, array $options = [])
{
try {
$response = $this->handler->send($this->handler->createRequest($method, $path, $options));
} catch (ClientException $ex) {
$response = $ex->getResponse();
}
return $response->json();
}
示例8: sendRequest
private function sendRequest()
{
$url = self::ApiUrl . '/' . $this->request;
$request = $this->client->createRequest('GET', $url);
$request->addHeader('Content-Type', 'application/json');
$response = $this->client->send($request);
$data = $response->json();
return new Response($data);
}
示例9: queryCircle
/**
* {@inheritdoc}
*/
public function queryCircle($url, $query_args = [], $method = 'GET', $request_options = [])
{
if (!isset($query_args['circle-token'])) {
throw new \InvalidArgumentException('The circle-token is required for all endpoints.');
}
$url .= '?' . http_build_query($query_args);
$request = $this->httpClient->createRequest($method, $url, $request_options);
$request->addHeaders(['Accept' => 'application/json']);
return $this->httpClient->send($request)->json();
}
示例10: post
/**
* {@inheritdoc }
*/
public function post($url, $body, array $files = array())
{
$request = $this->httpClient->createRequest('POST', $url, ['body' => $body]);
/** @var PostBodyInterface $postBody */
$postBody = $request->getBody();
foreach ($files as $key => $filePath) {
$file = $this->postFileFactory->create($key, $filePath);
$postBody->addFile($file);
}
$response = $this->httpClient->send($request);
return $response;
}
示例11: 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('POST', $endpoint, array('body' => $content, 'exceptions' => false, 'headers' => $headers));
$body = new PostBody();
if ($files && $request instanceof RequestInterface) {
foreach ($files as $name => $path) {
$body->addFile(new PostFile($name, fopen($path, 'r')));
}
}
$request->setBody($body);
$response = $this->client->send($request);
return new Response($response->getStatusCode(), $response->getBody());
}
示例12: request
/**
* {@inheritdoc}
*/
public function request($method, $url, $secret, array $params = [])
{
$options = ['auth' => [$secret, ''], 'connect_timeout' => 10, 'timeout' => 30, 'verify' => true];
if ('GET' !== $method) {
$options['body'] = $params;
}
$request = $this->client->createRequest($method, $url, $options);
try {
$rawResponse = $this->client->send($request);
} catch (RequestException $e) {
$rawResponse = $this->handleException($e);
}
return new Response($rawResponse->getStatusCode(), $rawResponse->getBody());
}
示例13: createGuzzleRequestFromRequest
/**
* @param RequestInterface $request
*
* @return \GuzzleHttp\Message\RequestInterface
*/
protected function createGuzzleRequestFromRequest(RequestInterface $request)
{
$headers = array();
foreach ($request->getHeaders() as $headerName => $headerValue) {
$headers[$headerName] = $headerValue[0];
}
$headers['X-Forwarded-For'] = $request->getClientIp();
$headers['X-Forwarded-Proto'] = $request->getScheme();
if (isset($headers['cookie'])) {
unset($headers['cookie']);
}
// see http://guzzle.readthedocs.org/en/latest/clients.html#request-options
$guzzleRequest = $this->client->createRequest($request->getMethod(), $request->getUri(), array('headers' => $headers, 'body' => $request->getBody(), 'query' => $request->getQueryParams(), 'cookies' => $request->getCookies(), 'exceptions' => false, 'decode_content' => true));
return $guzzleRequest;
}
示例14:
function it_should_post_an_url(ClientInterface $handler, RequestInterface $request, ResponseInterface $response)
{
$options = ['body' => 'foo'];
$handler->createRequest('POST', 'foo', $options)->willReturn($request);
$handler->send($request)->shouldBeCalled();
$handler->send($request)->willReturn($response);
$this->post('foo', $options);
}
示例15: createRequest
/**
* @param string $method
* @param array $payload
*
* @return RequestInterface
*/
private function createRequest($method, array $payload)
{
$request = $this->client->createRequest('POST');
$request->setUrl(self::API_BASE_URL . $method);
$body = new PostBody();
$body->replaceFields($payload);
$request->setBody($body);
return $request;
}