本文整理汇总了PHP中GuzzleHttp\ClientInterface::request方法的典型用法代码示例。如果您正苦于以下问题:PHP ClientInterface::request方法的具体用法?PHP ClientInterface::request怎么用?PHP ClientInterface::request使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类GuzzleHttp\ClientInterface
的用法示例。
在下文中一共展示了ClientInterface::request方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: loadSelf
private function loadSelf()
{
if ($this->_client && $this->_links && isset($this->_links['self']) && isset($this->_links['self']['href'])) {
$data = json_decode($this->_client->request('GET', $this->_links['self']['href'])->getBody(), true);
$this->setFromApiData($data);
}
}
示例2: callOpenpayClient
/**
* @param string $url
* @param array $options
* @param string $method
* @return array
* @throws OpenpayException
*/
protected function callOpenpayClient($url, array $options, $method = self::GET_METHOD)
{
try {
$rawResponse = $this->client->request($method, $url, $options);
} catch (\Exception $e) {
$responseParts = explode("\n", $e->getMessage());
$openpayException = new OpenpayException($responseParts[0], $e->getCode(), $e);
if (!is_null($e->getResponse())) {
$headers = $e->getResponse()->getHeaders();
}
$values['error_code'] = isset($headers['OP-Error-Code']) ? $headers['OP-Error-Code'][0] : null;
$values['request_id'] = isset($headers['OpenPay-Request-ID']) ? $headers['OpenPay-Request-ID'][0] : null;
$dictionary = OpenpayExceptionsDictionary::get();
if (isset($dictionary[$values['error_code']])) {
$values['description'] = $dictionary[$values['error_code']][self::DESCRIPTION_DICTIONARY_KEY];
}
if (isset($responseParts[self::EXCEPTION_RESPONSE_JSON_INDEX])) {
$responseObjectStr = $responseParts[self::EXCEPTION_RESPONSE_JSON_INDEX];
$responseObject = json_decode($responseObjectStr, self::JSON_DECODE_TO_ARRAY);
// sometimes openpay response is a malformed json
if (json_last_error() === JSON_ERROR_NONE) {
$values = array_merge($values, $responseObject);
}
$openpayException = $this->exceptionMapper->create($values, $openpayException);
}
throw $openpayException;
}
$responseContent = $rawResponse->getBody()->getContents();
$responseArray = json_decode($responseContent, self::JSON_DECODE_AS_ARRAY);
return $responseArray;
}
示例3: sendRequest
/**
* Send request NIK
*
* @param string $nik
* @return \Psr\Http\Message\ResponseInterface
* @throws \InvalidArgumentException
* @throws \GuzzleHttp\Exception\GuzzleException
*/
public function sendRequest($nik)
{
// Verify NIK
$nik = $this->assertValidateNik($nik);
$form_params = ['nik_global' => $nik, 'g-recaptcha-response' => ' ', 'wilayah_id' => '0', 'cmd' => 'Cari.'];
return $this->client->request('POST', self::END_POINT, compact('form_params'));
}
示例4: sendRequest
/**
* @param string $method
* @param string $endpoint
* @param array $options
* @return \stdClass
*/
protected function sendRequest($method, $endpoint, array $options = [])
{
$this->assertHasAccessToken();
$opts = array_merge_recursive(['headers' => ['Authorization' => 'Bearer ' . $this->accessToken]], $options);
$res = $this->httpClient->request($method, self::API_URL . $endpoint, $opts);
return json_decode((string) $res->getBody());
}
示例5: fetch
/**
* Fetches data from api
* @param string $url
* @param array $options
* @throws ConnectionException
* @throws HTTPException
* @return string
*/
public function fetch($url, $options)
{
$request_type = $this->config->http_post === true ? 'form_params' : 'query';
$options = [$request_type => $options];
try {
$response = $this->client->request($this->config->http_post === true ? 'POST' : 'GET', $url, $options);
} catch (GuzzleException $exception) {
throw new ConnectionException($exception->getMessage(), $exception->getCode());
}
if ($response->getStatusCode() >= 400) {
// ccp is using error codes even if they send a valid application
// error response now, so we have to use the content as result
// for some of the errors. This will actually break if CCP ever uses
// the HTTP Status for an actual transport related error.
switch ($response->getStatusCode()) {
case 400:
case 403:
case 500:
case 503:
return $response->getBody()->getContents();
break;
}
throw new HTTPException($response->getStatusCode(), $url);
}
return $response->getBody()->getContents();
}
示例6: get
public function get($url)
{
if (!$this->isValidArgument($url)) {
throw new \InvalidArgumentException('Supply a valid URL please.');
}
return $this->guzzle->request("GET", $url);
}
示例7: call
/**
* @param $id
* @return Result
*/
protected function call($method, $resource, $body = null, $acceptedCodes = array(200))
{
try {
$response = $this->client->request($method, $resource, array('body' => $body));
$responseBody = (string) $response->getBody();
if ($responseBody) {
/** @var Result $result */
$result = $this->serializer->deserialize($responseBody, $this->getResultClass(), 'json');
$result->deserializeData($this->serializer, $this->getModel());
} else {
$result = new Result();
}
$result->setSuccess(in_array($response->getStatusCode(), $acceptedCodes))->setMessage($response->getReasonPhrase());
return $result;
} catch (GuzzleException $ge) {
if ($ge->getCode() == \Symfony\Component\HttpFoundation\Response::HTTP_TOO_MANY_REQUESTS && php_sapi_name() == "cli") {
sleep(5);
return $this->call($method, $resource, $body, $acceptedCodes);
} else {
$result = new Result();
$result->setSuccess(false)->setMessage(sprintf("Client error: %s", $ge->getMessage()));
return $result;
}
} catch (\Exception $e) {
$result = new Result();
$result->setSuccess(false)->setMessage(sprintf("General error: %s", $e->getMessage()));
return $result;
}
}
示例8:
function it_should_get_a_raw_response(ClientInterface $handler, ResponseInterface $response)
{
$handler->request('GET', 'foo', [])->shouldBeCalled();
$handler->request('GET', 'foo', [])->willReturn($response);
$response->getBody()->shouldBeCalled();
$this->get('foo', [], false);
}
示例9: request
/**
* {@inheritDoc}
*/
public function request($method, $uri, array $parameters = [])
{
$itemEnvelope = null;
if (isset($parameters['itemEnvelope'])) {
$itemEnvelope = $parameters['itemEnvelope'];
unset($parameters['itemEnvelope']);
}
try {
$response = $this->guzzle->request($method, $uri, array_merge($this->params, $parameters));
} catch (ClientException $e) {
if (!$e->hasResponse()) {
throw $e;
}
throw $this->resolveExceptionClass($e);
} catch (Exception $e) {
throw $e;
}
$response = json_decode($response->getBody()->getContents(), true);
if ($response === null) {
$response = [];
}
if ($itemEnvelope) {
$response['itemEnvelope'] = $itemEnvelope;
}
return Response::createFromJson($response);
}
示例10: send
/**
* @return TokenResponse
*/
public function send()
{
$url = $this->serverConfig->getParams()['token_endpoint'];
$params = [['name' => 'grant_type', 'contents' => self::GRANT_TYPE], ['name' => 'refresh_token', 'contents' => $this->refreshToken]];
$response = $this->httpClient->request('POST', $url, ['multipart' => $params]);
return new TokenResponse($response);
}
示例11: it_includes_parameters
public function it_includes_parameters(HttpClient $client, Response $response)
{
$url = 'http://www.example.com';
$params = ['foo' => 'bar'];
$client->request('get', $url, ['query' => $params])->shouldBeCalled();
$client->request('get', $url, ['query' => $params])->willReturn($response);
$this->get($url, $params)->shouldHaveType(GuzzleResponseAdapter::class);
}
示例12: request
/**
* Will perform a $verb request to the given $endpoint with $parameters.
*
* @param string $endpoint Url where curly braces will be replaces, Ex: add/{id}/something
* @param string $verb May be get, delete, head, options, patch, post, put
* @param array $parameters Array of parameters, Ex: ['id' => 5, 'name' => 'john doe']
*
* @throws RequestException
*
* @return array Response data
*/
public function request($endpoint, $verb = 'get', array $parameters = [])
{
try {
return $this->client->request($verb, $endpoint, $parameters);
} catch (GuzzleException $error) {
throw new RequestException((string) $error->getResponse()->getBody(), $error->getCode());
}
}
示例13: getAccessToken
/**
* @param string $apiKey
* @param string $apiSecret
* @param string $username
* @param string $password
*
* @return AccessToken
*/
public function getAccessToken($apiKey, $apiSecret, $username, $password)
{
$response = $this->guzzle->request('POST', self::ENDPOINT, ['auth' => [$apiKey, $apiSecret], 'form_params' => ['grant_type' => 'password', 'username' => $username, 'password' => $password]]);
$data = json_decode((string) $response->getBody(), true);
$token = $data['access_token'];
$expiry = new \DateTime('@' . (time() + $data['expires_in']));
return new AccessToken($token, $expiry);
}
示例14: __call
/**
* Calls the method contained in the actions of this resource
* @param string $method
* @param array $args
* @return \StdClass The JSON decoded response
*/
public function __call($method, $args)
{
$action = $this->getActions()[$method];
$uri = $this->getBaseUri() . $this->getPath($action, $args);
$params = $this->getParams($args);
$this->last_response = $this->client->request($action['method'], $uri, $params);
return json_decode($this->last_response->getBody());
}
示例15: request
/**
* Perform the HTTP request
*
* @param string $method HTTP method/verb
* @param string $url URL to send the request
* @param array $parameters Key/Value pairs to form the query string
* @param array $options Options to pass straight to GuzzleClient
*
* @return \Pisa\GizmoAPI\Adapters\GuzzleResponseAdapter
*/
public function request($method, $url, array $parameters = [], array $options = [])
{
if (!empty($parameters)) {
$options['query'] = $this->fixParameters($parameters);
}
$response = $this->client->request($method, $url, $options);
return new HttpResponse($response);
}