本文整理汇总了PHP中GuzzleHttp\Message\ResponseInterface::json方法的典型用法代码示例。如果您正苦于以下问题:PHP ResponseInterface::json方法的具体用法?PHP ResponseInterface::json怎么用?PHP ResponseInterface::json使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类GuzzleHttp\Message\ResponseInterface
的用法示例。
在下文中一共展示了ResponseInterface::json方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: populateFromResponse
/**
* {@inheritDoc}
*/
public function populateFromResponse(ResponseInterface $response)
{
$entries = $response->json()['access']['serviceCatalog'];
foreach ($entries as $entry) {
$this->entries[] = $this->model('Entry', $entry);
}
}
示例2: __construct
/**
* Creates a response object
* @param ResponseInterface $responseInterface
* @throws ParseException
*/
public function __construct(ResponseInterface $responseInterface)
{
$this->response = $responseInterface->json();
$this->status = isset($this->response['meta']['code']) ? $this->response['meta']['code'] : 500;
$this->errorCode = isset($this->response['meta']['error_code']) ? $this->response['meta']['error_code'] : null;
$this->responseErrorCodes = new ResponseErrorCodes();
}
示例3: handleResponse
/**
* @param ResponseInterface $response
*/
private function handleResponse(ResponseInterface $response, Request $request)
{
$context = $this->prepareBaseContext($request);
$data = $response->json();
if (isset($data['results'])) {
foreach ($data['results'] as $registrationIdIdx => $result) {
$context['registrationId'] = $this->registrationIds[$registrationIdIdx];
$context['result'] = var_export($result, true);
switch (key($result)) {
case 'message_id':
if (isset($result['registration_id'])) {
$this->callback(EventsEnum::ON_PUSH_SUCCESS_BUT_NEED_NEW_ID, [$context]);
} else {
$this->callback(EventsEnum::ON_PUSH_SUCCESS, [$context]);
}
break;
case 'error':
$currentResult = current($result);
switch ($currentResult) {
case 'Unavailable':
$this->callback(EventsEnum::ON_PUSH_UNAVAILABLE, [$context]);
break;
case 'InvalidRegistration':
$this->callback(EventsEnum::ON_PUSH_INVALID_REGISTRATION, [$context]);
break;
case 'NotRegistered':
$this->callback(EventsEnum::ON_PUSH_NOT_REGISTERED, [$context]);
break;
}
break;
}
}
}
return ['success' => $data['success'], 'failure' => $data['failure']];
}
示例4: decodeHttpResponse
/**
* Decode an HTTP Response.
* @static
* @throws Google_Service_Exception
* @param GuzzleHttp\Message\RequestInterface $response The http response to be decoded.
* @param GuzzleHttp\Message\ResponseInterface $response
* @return mixed|null
*/
public static function decodeHttpResponse(ResponseInterface $response, RequestInterface $request = null)
{
$body = (string) $response->getBody();
$code = $response->getStatusCode();
$result = null;
// return raw response when "alt" is "media"
$isJson = !($request && 'media' == $request->getQuery()->get('alt'));
// set the result to the body if it's not set to anything else
if ($isJson) {
try {
$result = $response->json();
} catch (ParseException $e) {
$result = $body;
}
} else {
$result = $body;
}
// retry strategy
if (intVal($code) >= 300) {
$errors = null;
// Specific check for APIs which don't return error details, such as Blogger.
if (isset($result['error']) && isset($result['error']['errors'])) {
$errors = $result['error']['errors'];
}
throw new Google_Service_Exception($body, $code, null, $errors);
}
return $result;
}
示例5: it_handle_unexpected_oboom_response
public function it_handle_unexpected_oboom_response(Client $guzzleClient, ResponseInterface $response)
{
$url = 'http://example.com';
$response->json()->shouldBeCalled()->willReturn([400]);
$guzzleClient->get($url, ['query' => []])->willReturn($response);
$this->shouldThrow(new RuntimeException('API error', 400))->during('call', [$url]);
}
示例6: getCount
public function getCount($content, ResponseInterface $response = null)
{
$data = $response->json();
if (empty($data)) {
return false;
}
return (int) array_shift($data)['shares'];
}
示例7:
function it_should_return_results_as_an_object(ClientInterface $client, ResponseInterface $response)
{
$params = 'key=' . $this->test_key . '&hero_id=12345&skill=2';
$expectedUrl = $this->test_base_url . 'IDOTA2Match_570/GetMatchDetails/v001/?' . $params;
$responseReturn = json_decode('{"result": {"status": 1, "num_results": 0, "total_results": 407, "results_remaining": 307} }');
$response->json(['object' => true])->shouldBeCalled()->willReturn($responseReturn);
$client->get($expectedUrl)->shouldBeCalled()->willReturn($response);
$this->endpoint('IDOTA2Match_570/GetMatchDetails/v001')->options(['hero_id' => 12345, 'skill' => 2])->get()->shouldReturn($responseReturn);
}
示例8: before
public function before(CommandInterface $command, ResponseInterface $response, Parameter $model, &$result, array $context = [])
{
$this->json = $response->json() ?: [];
// relocate named arrays, so that they have the same structure as
// arrays nested in objects and visit can work on them in the same way
if ($model->getType() == 'array' && ($name = $model->getName())) {
$this->json = [$name => $this->json];
}
}
示例9: populateFromResponse
/**
* Populates the current resource from a response object.
*
* @param ResponseInterface $response
*
* @return $this|ResourceInterface
*/
public function populateFromResponse(ResponseInterface $response)
{
if (strpos($response->getHeader('Content-Type'), 'application/json') === 0) {
$json = $response->json();
if (!empty($json)) {
$this->populateFromArray($this->flatten($json));
}
}
return $this;
}
示例10: __construct
public function __construct(ResponseInterface $response)
{
try {
$this->details = $response->json();
$message = isset($this->details['error']['message']) ? $this->details['error']['message'] : $response->getReasonPhrase();
} 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());
}
示例11: theResponseShouldContainJson
/**
* Checks that response body contains JSON from PyString.
*
* Do not check that the response body /only/ contains the JSON from PyString,
*
* @param PyStringNode $jsonString
*
* @throws \RuntimeException
*
* @Then /^(?:the )?response should contain json:$/
*/
public function theResponseShouldContainJson(PyStringNode $jsonString)
{
$etalon = json_decode($this->replacePlaceHolder($jsonString->getRaw()), true);
$actual = $this->response->json();
if (null === $etalon) {
throw new \RuntimeException("Can not convert etalon to json:\n" . $this->replacePlaceHolder($jsonString->getRaw()));
}
$factory = new SimpleFactory();
$matcher = $factory->createMatcher();
Assertions::assertTrue($matcher->match($actual, $etalon));
}
示例12: assertResponseMatch
/**
* Asserts response match with the response schema.
*
* @param ResponseInterface $response
* @param SchemaManager $schemaManager
* @param string $path percent-encoded path used on the request.
* @param string $httpMethod
* @param string $message
*/
public function assertResponseMatch(ResponseInterface $response, SchemaManager $schemaManager, $path, $httpMethod, $message = '')
{
$this->assertResponseMediaTypeMatch($response->getHeader('Content-Type'), $schemaManager, $path, $httpMethod, $message);
$httpCode = $response->getStatusCode();
$headers = $response->getHeaders();
foreach ($headers as &$value) {
$value = implode(', ', $value);
}
$this->assertResponseHeadersMatch($headers, $schemaManager, $path, $httpMethod, $httpCode, $message);
$this->assertResponseBodyMatch($response->json(['object' => true]), $schemaManager, $path, $httpMethod, $httpCode, $message);
}
示例13: __construct
public function __construct(RequestInterface $request, ResponseInterface $response)
{
$payload = $response->json();
$this->id = $payload['id'];
$result = $payload['result'];
$this->status = [];
if (isset($result['status'])) {
$this->status = $result['status'];
unset($result['status']);
}
parent::__construct($result);
}
示例14: theResponseShouldContainJson
/**
* Checks that response body contains JSON from PyString.
*
* Do not check that the response body /only/ contains the JSON from PyString,
*
* @param PyStringNode $jsonString
*
* @throws \RuntimeException
*
* @Then /^(?:the )?response should contain json:$/
*/
public function theResponseShouldContainJson(PyStringNode $jsonString)
{
$etalon = json_decode($this->replacePlaceHolder($jsonString->getRaw()), true);
$actual = $this->response->json();
if (null === $etalon) {
throw new \RuntimeException("Can not convert etalon to json:\n" . $this->replacePlaceHolder($jsonString->getRaw()));
}
Assertions::assertGreaterThanOrEqual(count($etalon), count($actual));
foreach ($etalon as $key => $needle) {
Assertions::assertArrayHasKey($key, $actual);
Assertions::assertEquals($etalon[$key], $actual[$key]);
}
}
示例15: __construct
/**
* Creates a new Funnelback response.
*
* @param \GuzzleHttp\Message\ResponseInterface $http_response
* The http response.
*/
public function __construct(ResponseInterface $http_response)
{
$this->httpResponse = $http_response;
$this->responseJson = $http_response->json();
$this->query = $this->responseJson['question']['query'];
$response = $this->responseJson['response'];
$this->returnCode = $response['returnCode'];
$this->totalTimeMillis = $response['performanceMetrics']['totalTimeMillis'];
$result_packet = $response['resultPacket'];
$this->resultsSummary = new ResultSummary($result_packet['resultsSummary']);
$this->results = $this->buildResults($result_packet['results']);
$this->facets = $this->buildFacets($response['facets']);
}