本文整理汇总了PHP中GuzzleHttp\Message\Response::json方法的典型用法代码示例。如果您正苦于以下问题:PHP Response::json方法的具体用法?PHP Response::json怎么用?PHP Response::json使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类GuzzleHttp\Message\Response
的用法示例。
在下文中一共展示了Response::json方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getFailedReason
/**
* get the response phrase
* @return array
*/
public function getFailedReason()
{
if ($this->isFailed()) {
$error = $this->response->json();
return array_key_exists('error', $error) ? $error['error'] : array();
}
return null;
}
示例2: deduce
/**
* @return ErrorResponse|SuccessResponse
*/
public function deduce()
{
/* @var array $response */
$response = (array) $this->response->json();
if (array_key_exists('type', $response) && $response['type'] === 'error') {
return new ErrorResponse($this->response->getStatusCode(), $this->response->getHeaders(), $this->response->getBody());
}
return new SuccessResponse($this->response->getStatusCode(), $this->response->getHeaders(), $this->response->getBody());
}
示例3: getDecodedJson
/**
* @return array
*/
private function getDecodedJson()
{
if (!$this->decoded) {
try {
$this->decoded = $this->response->json();
} catch (ParseException $e) {
return array();
}
}
return $this->decoded;
}
示例4: checkResponse
/**
* Check the return of a request response
*
* @param Response $response
* @return false if error, decoded response if valid status
* @author Niko
*/
private function checkResponse(Response $response)
{
if ($response->getStatusCode() != 200) {
$res = $response->json();
var_dump($response->getStatusCode());
if (isset($res['Error'])) {
$this->log($response->getStatusCode(), $res['Error']);
}
return false;
}
return $response->json();
}
示例5: parseBody
/**
* Parse body of response
*
* @return Crawler | array | string
*/
private function parseBody()
{
$type = $this->getType();
$body = (string) $this->resource->getBody();
if (!$body) {
return '';
}
if (in_array($type, ['text/html', 'text/xml'])) {
return new Crawler($body);
}
if ($type === 'application/json') {
return $this->resource->json();
}
return $body;
}
示例6: wrapResponse
private static function wrapResponse(Response $response)
{
$payload = $response->json(["object" => true]);
// strip the envelope
if (isset($payload->data) && isset($payload->meta)) {
return new static($payload->data);
}
return new static($payload);
}
示例7: __construct
public function __construct(Response $response)
{
try {
$this->details = $response->json();
$message = $this->details['error']['message'];
} catch (ParseException $e) {
$message = $response->getReasonPhrase();
}
parent::__construct(sprintf('The request failed and returned an invalid status code ("%d") : %s', $response->getStatusCode(), $message), $response->getStatusCode());
}
示例8: Payment
function it_maps_declined_response_402_to_payment_result_object(ClientException $exception, Response $response, ResultObjectMapperInterface $resultMapper, MethodInterface $method)
{
$result = ['error' => 'Error string'];
$resultObject = new Payment();
$response->getStatusCode()->willReturn(402);
$response->json()->willReturn($result);
$method->createResultObject()->willReturn($resultObject);
$exception->getResponse()->willReturn($response);
$resultMapper->map($result, $resultObject)->shouldBeCalled()->willReturn($resultObject);
$this->get($exception, $method)->getResult()->shouldReturn($resultObject);
}
示例9: testCanReadFindAddressResponse
public function testCanReadFindAddressResponse()
{
$json = file_get_contents(__DIR__ . '\\GetAddressIO.json');
$response = new Response(200, [], Stream::factory($json));
$json = $response->json();
$address = new Address();
$address->setLatitude($json['Latitude'])->setLongitude($json['Longitude'])->setStreet($json['Addresses'][0]);
$this->assertEquals($address->getLatitude(), '51.503038');
$this->assertEquals($address->getLongitude(), '-0.128371');
$this->assertEquals($address->getStreet(), 'Prime Minister & First Lord of the Treasury, 10 Downing Street, , , , London, Greater London');
}
示例10: parseResponse
/**
* Parse the returned response.
*
* @param \GuzzleHttp\Message\Response $response
* @return array
*
* @throws \RuntimeException
*/
protected function parseResponse(Response $response)
{
$contentType = explode(';', $response->getHeader('content-type'))[0];
switch ($contentType) {
case 'text/javascript':
case 'application/json':
return $response->json();
case 'application/xml':
return $response->xml();
}
throw new \RuntimeException("Unsupported returned content-type [{$contentType}]");
}
示例11: handleResponse
/**
* @param \GuzzleHttp\Message\Response $response
* @return Error
*/
protected function handleResponse($response)
{
$contentType = $response->getHeader('content-type');
if (stripos($contentType, 'application/json') === false) {
return $response;
}
$json = $response->json();
if ($response->getStatusCode() >= 400) {
// oops an error with the response
return new Error($response->getStatusCode(), $json['code'], $json['message']);
}
return $json;
}
示例12: testCanReadFindAddressResponse
public function testCanReadFindAddressResponse()
{
$json = file_get_contents(__DIR__ . '\\PostcodeData.json');
$response = new Response(200, [], Stream::factory($json));
$json = $response->json();
$address = new Address();
$address->setStreet($json['details'][0]['street'])->setTown($json['details'][0]['city'])->setMunicipality($json['details'][0]['municipality'])->setProvince($json['details'][0]['province'])->setLatitude($json['details'][0]['lat'])->setLongitude($json['details'][0]['lon']);
$this->assertEquals($address->getStreet(), 'Evert van de Beekstraat');
$this->assertEquals($address->getTown(), 'Schiphol');
$this->assertEquals($address->getMunicipality(), 'Haarlemmermeer');
$this->assertEquals($address->getProvince(), 'Noord-Holland');
$this->assertEquals($address->getLatitude(), '52.3035437835548');
$this->assertEquals($address->getLongitude(), '4.7474064734608');
}
示例13: handleResponse
/**
* @param Response $response
* @return array|mixed
* @throws JsonApiResponseException
*/
public function handleResponse($response)
{
$contentType = $response->getHeader('content-type');
// if its not json, then just return the response and handle it in your own object.
if (stripos($contentType, 'application/json') === false) {
return $response;
}
$json = $response->json();
// adobe says hey this didn't work!
if ($response->getStatusCode() >= 400) {
// oops an error with the response, from Adobe complaining about something in your code.
throw new JsonApiResponseException($response->getStatusCode(), $json['message'], $json['code']);
}
return $json;
}
示例14: sendRequest
/**
* Making request using Guzzle
*
* @param string $method Type of request, either POST, GET, PUT or DELETE
* @param string $endpoint The operation / task for API
* @param array $data The parameter need to be passed
* @param array $options The options like header, body, etc
*
* @return EntityBodyInterface|string
* @throws \Exception
*/
private function sendRequest($method, $endpoint, array $data = array(), array $options = array())
{
$uri = $this->buildUri($endpoint);
if ($method === "GET" || $method === "PUT") {
$uri = $this->buildUri($endpoint, $data);
} elseif (count($data)) {
$options['body'] = $data;
}
$this->request = $this->client->createRequest($method, $uri, $options);
$this->response = $this->client->send($this->request);
if ($this->response->getStatusCode() >= 400) {
$bt = debug_backtrace();
$caller = $bt[2];
if (isset($caller['class']) && $caller['class'] === get_class(new StacklaModel())) {
$json = (string) $this->response->getBody();
if (JsonValidator::validate($json, true)) {
$content = json_decode($json, true);
if (isset($content['errors'])) {
$caller['object']->fromArray($content);
}
}
}
if ($this->logger) {
$this->logger->addError('-> REQUEST [' . $this->request->getMethod() . ' - ' . $this->request->getUrl() . "]", array($this->request->getMethod() !== "GET" ? (string) $this->request->getBody() : ""));
$this->logger->addError('<- RESPONSE [' . $this->response->getStatusCode() . ':' . $this->response->getReasonPhrase() . "]", array((string) $this->response->getBody()));
}
} else {
if ($this->logger) {
$this->logger->addInfo('-> REQUEST [' . $this->request->getMethod() . ' - ' . $this->request->getUrl() . "]", array($this->request->getMethod() !== "GET" ? (string) $this->request->getBody() : ""));
$this->logger->addInfo('<- RESPONSE [' . $this->response->getStatusCode() . ':' . $this->response->getReasonPhrase() . "]", array($this->response->json()));
}
}
$statusCode = $this->response->getStatusCode();
switch ($statusCode) {
case 200:
return (string) $this->response->getBody();
case 400:
throw ApiException::create(sprintf("Server return %s error code. Bad request: The request could not be understood. %s", $this->response->getStatusCode(), (string) $this->response->getBody()), $statusCode, (string) $this->response->getBody());
case 401:
throw ApiException::create(sprintf("Server return %s error code. Unauthorized: Authentication credentials invalid or not authorised to access resource", $this->response->getStatusCode()), $statusCode, (string) $this->response->getBody());
case 403:
throw ApiException::create(sprintf("\n Server return %s error code. Rate limit exceeded: Too many requests in the current time window", $this->response->getStatusCode()), $statusCode, (string) $this->response->getBody());
case 404:
throw ApiException::create(sprintf("Server return %s error code. Invalid resource: Invalid resource specified or resource not found", $this->response->getStatusCode()), $statusCode, (string) $this->response->getBody());
default:
throw ApiException::create(sprintf("Server return %s error code.Server error: An error on the server prohibited a successful response; please contact support. %s", $this->response->getStatusCode(), (string) $this->response->getBody()), $statusCode, (string) $this->response->getBody());
}
}
示例15: parseBody
/**
* {@inheritdoc}
*/
public function parseBody(Response $response, $format = '')
{
$output = NULL;
if (empty($format)) {
$format = isset($this->defaults['form']) ? $this->defaults['form'] : NULL;
}
if ($format == $this::FORMAT_JSON || $format == $this::FORMAT_CJSON) {
$output = $response->json();
}
if ($format == $this::FORMAT_ATOM || $format == $this::FORMAT_RSS) {
$output = $this->xmlToArray($response->xml());
}
if ($output['isException']) {
throw new MpxException(sprintf('Exception returned: %s', print_r($output, TRUE)), $output['responseCode']);
}
if (isset($output)) {
return $output;
}
throw new MpxException('Custom formats are not supported.');
}