本文整理汇总了PHP中Guzzle\Http\Message\Response::json方法的典型用法代码示例。如果您正苦于以下问题:PHP Response::json方法的具体用法?PHP Response::json怎么用?PHP Response::json使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Guzzle\Http\Message\Response
的用法示例。
在下文中一共展示了Response::json方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getResponse
/**
* Get the response of the query.
* Will be the json decoded data if success, else the error message.
*
* @return array|string
*/
public function getResponse()
{
if (false === $this->response->isSuccessful()) {
return $this->response->getMessage();
}
return $this->response->json();
}
示例2: genderize
/**
* @param array $query
*
* @return array
*/
public function genderize(array $query)
{
$queryString = '?' . http_build_query($query);
$request = $this->get($queryString);
$this->setLastResponse($request->send());
return $this->lastResponse->json();
}
示例3: getResponseAsArray
/**
* Get response as an array.
*
* @return array
*/
private function getResponseAsArray()
{
$this->result = $this->response->json();
if ($this->responseHasErrors()) {
return false;
}
return $this->result;
}
示例4: extractFromResponse
/**
* @param string $what
* @return null
*/
protected function extractFromResponse($what)
{
$data = $this->guzzleResponse->json();
if ($data && is_array($data)) {
if (array_key_exists($what, $data)) {
return $data[$what];
}
}
return null;
}
示例5: __construct
public function __construct(\Guzzle\Http\Message\Response $response, $type)
{
$this->_response = $response;
// if json returned - parse and fill structure
$contentType = $this->_response->getContentType();
if (strpos($contentType, ';')) {
list($contentType, ) = explode(';', $contentType);
}
if ($contentType == 'application/json') {
$this->_structure = new $type($this->_response->json());
} else {
throw new \Exception('Structure parser for content type "' . $this->_response->getContentType() . '" not implemented');
}
}
示例6: factory
/**
* Simple exception factory for creating Intercom standardised exceptions
*
* @param RequestInterface $request The Request
* @param Response $response The response
* @return BadResponseException
*/
public static function factory(RequestInterface $request, Response $response)
{
$response_json = $response->json();
$intercom_unavailable_error = NULL;
if (!static::isValidIntercomError($response_json)) {
if ($response->isServerError()) {
$label = 'Server error response';
$class = __NAMESPACE__ . '\\ServerErrorResponseException';
$intercom_unavailable_error = 'Service Unavailable: Back-end server is at capacity';
} else {
$label = 'Unsuccessful response';
$class = __CLASS__;
}
} elseif ($response->isClientError()) {
$label = 'Client error response';
$class = __NAMESPACE__ . '\\ClientErrorResponseException';
} elseif ($response->isServerError()) {
$label = 'Server error response';
$class = __NAMESPACE__ . '\\ServerErrorResponseException';
} else {
$label = 'Unsuccessful response';
$class = __CLASS__;
}
$message = $label . PHP_EOL . implode(PHP_EOL, array('[status code] ' . $response->getStatusCode(), '[reason phrase] ' . $response->getReasonPhrase(), '[url] ' . $request->getUrl()));
$e = new $class($message);
$e->setResponse($response);
$e->setRequest($request);
// Sets the errors if the error response is the standard Intercom error type
if (static::isValidIntercomError($response_json)) {
$e->setErrors($response_json['errors']);
} elseif ($intercom_unavailable_error != NULL) {
$e->setErrors(array('code' => 'service_unavailable', "message" => $intercom_unavailable_error));
}
return $e;
}
示例7: factory
/**
* Simple exception factory for creating Intercom standardised exceptions
*
* @param RequestInterface $request The Request
* @param Response $response The response
* @return BadResponseException
*/
public static function factory(RequestInterface $request, Response $response)
{
if (!static::isValidIntercomError($response->json())) {
$label = 'Unsuccessful response';
$class = __CLASS__;
} elseif ($response->isClientError()) {
$label = 'Client error response';
$class = __NAMESPACE__ . '\\ClientErrorResponseException';
} elseif ($response->isServerError()) {
$label = 'Server error response';
$class = __NAMESPACE__ . '\\ServerErrorResponseException';
} else {
$label = 'Unsuccessful response';
$class = __CLASS__;
}
$message = $label . PHP_EOL . implode(PHP_EOL, array('[status code] ' . $response->getStatusCode(), '[reason phrase] ' . $response->getReasonPhrase(), '[url] ' . $request->getUrl()));
$e = new $class($message);
$e->setResponse($response);
$e->setRequest($request);
// Sets the errors if the error response is the standard Intercom error type
if (static::isValidIntercomError($response->json())) {
$e->setErrors($response->json()['errors']);
}
return $e;
}
示例8: parseResponseIntoArray
/**
* Parses response into an array
*
* @param Response $response
* @return array
*/
protected function parseResponseIntoArray($response)
{
if (strpos($response->getContentType(), 'json') === false) {
parse_str($response->getBody(true), $array);
return $array;
}
return $response->json();
}
示例9: json
/**
* {@inheritdoc}
*/
public function json()
{
try {
$result = $this->response->json();
} catch (\Exception $exception) {
throw GuzzleRestException::createFromException($exception);
}
return $result;
}
示例10: formatResponse
/**
* Format the raw API response into an array of Models.
*
* @param Guzzle\Http\Message\Response $response api response
* @param string $model the name of the class to instantiate
*
* @return array
*/
public function formatResponse(Response $response, $model)
{
$body = $response->json();
$collection = [];
foreach ($body['data'] as $item) {
$collection[] = new $model($this, $item);
}
// foreach body[data]
return $collection;
}
示例11: decodeResponse
public function decodeResponse(Response $response)
{
$jsonResponse = $response->json();
if (isset($jsonResponse["result"]) && ($result = $jsonResponse["result"]) !== "success") {
throw new RpcErrorException($result);
}
if (!isset($jsonResponse["arguments"])) {
throw new RpcNoResultException();
}
return $jsonResponse["arguments"];
}
示例12: decodeResponse
public function decodeResponse(Response $response)
{
$json = $response->json();
if (isset($json["error"])) {
throw new RpcErrorException($json["error"]);
}
if (!isset($json["result"])) {
throw new RpcNoResultException();
}
return $json["result"];
}
示例13: testMagicOperationsReturnResult
/**
* Executing service description defined operations returns the result
* @group unit
*/
public function testMagicOperationsReturnResult()
{
$responseData = array("this" => "that");
Phockito::when($this->mockResponse->getStatusCode())->return(200);
Phockito::when($this->mockResponse->json())->return($this->testDescription);
Phockito::when($this->mockRequest->send())->return($this->mockResponse);
Phockito::when($this->client)->get(anything())->return($this->mockRequest);
Phockito::when($this->client)->callParent(anything(), anything())->return($responseData);
$result = $this->client->SomeOperation(array());
$this->assertEquals($result, $responseData);
}
示例14: testSuccess
public function testSuccess()
{
$body = file_get_contents(dirname(dirname(__FILE__)) . '/Mock/TokenPurchaseSuccess.txt');
$httpResponse = new Response(200, array('Content-Type' => 'application/json'), $body);
$response = new TokenPurchaseResponse($this->getMockRequest(), $httpResponse->json());
$this->assertTrue($response->isSuccessful());
$this->assertFalse($response->isRedirect());
$this->assertNull($response->getCode());
$this->assertNull($response->getMessage());
$this->assertSame('205182114555', $response->getTransactionReference());
}
示例15: __construct
/**
* @param \Guzzle\Http\Message\Response $response
*/
public function __construct(\Guzzle\Http\Message\Response $response)
{
if ($response->isSuccessful()) {
$this->response = $response->json();
foreach ($this->response as $key => $value) {
$this->response[$key] = $value;
}
} else {
// TODO: Error handling
}
}