本文整理汇总了PHP中GuzzleHttp\Message\ResponseInterface::getBody方法的典型用法代码示例。如果您正苦于以下问题:PHP ResponseInterface::getBody方法的具体用法?PHP ResponseInterface::getBody怎么用?PHP ResponseInterface::getBody使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类GuzzleHttp\Message\ResponseInterface
的用法示例。
在下文中一共展示了ResponseInterface::getBody方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: _getResult
/**
* @param ResponseInterface $response
*
* @return ApiResult
*/
protected function _getResult($response)
{
if (!$response instanceof ResponseInterface) {
throw new \InvalidArgumentException("{$response} should be an instance of ResponseInterface");
}
$result = new ApiResult();
$result->setStatusCode($response->getStatusCode());
$callId = $response->getHeader('X-Call-Id');
if (!empty($callId)) {
$result->setCallId($callId);
}
$decoded = json_decode((string) $response->getBody());
if (isset($decoded->meta) && isset($decoded->data) && isset($decoded->meta->code) && $decoded->meta->code == $response->getStatusCode()) {
$meta = $decoded->meta;
$data = $decoded->data;
if (isset($meta->message)) {
$result->setStatusMessage($meta->message);
}
$result->setContent(json_encode($data));
} else {
$result->setContent((string) $response->getBody());
}
$result->setHeaders($response->getHeaders());
return $result;
}
示例2: save
public function save(RequestInterface $request, ResponseInterface $response)
{
$ttl = (int) $request->getConfig()->get('cache.ttl');
$key = $this->getCacheKey($request);
// Persist the response body if needed
if ($response->getBody() && $response->getBody()->getSize() > 0) {
$this->cache->save($key, array('code' => $response->getStatusCode(), 'headers' => $response->getHeaders(), 'body' => (string) $response->getBody()), $ttl);
$request->getConfig()->set('cache.key', $key);
}
}
示例3: theCaldavHttpStatusCodeShouldBe
/**
* @Then The CalDAV HTTP status code should be :code
*/
public function theCaldavHttpStatusCodeShouldBe($code)
{
if ((int) $code !== $this->response->getStatusCode()) {
throw new \Exception(sprintf('Expected %s got %s', (int) $code, $this->response->getStatusCode()));
}
$body = $this->response->getBody()->getContents();
if ($body && substr($body, 0, 1) === '<') {
$reader = new Sabre\Xml\Reader();
$reader->xml($body);
$this->responseXml = $reader->parse();
}
}
示例4: formatBody
/**
* @param RequestInterface|ResponseInterface $message
* @return string
*/
protected function formatBody($message)
{
$header = $message->getHeader('Content-Type');
if (JsonStringFormatter::isJsonHeader($header)) {
$formatter = new JsonStringFormatter();
return $formatter->format($message->getBody());
} elseif (XmlStringFormatter::isXmlHeader($header)) {
$formatter = new XmlStringFormatter();
return $formatter->format($message->getBody());
}
$factory = new StringFactoryFormatter();
return $factory->format($message->getBody());
}
示例5: setStatusTestingApp
protected function setStatusTestingApp($enabled)
{
$this->sendingTo($enabled ? 'post' : 'delete', '/cloud/apps/testing');
$this->theHTTPStatusCodeShouldBe('200');
$this->theOCSStatusCodeShouldBe('100');
$this->sendingTo('get', '/cloud/apps?filter=enabled');
$this->theHTTPStatusCodeShouldBe('200');
if ($enabled) {
PHPUnit_Framework_Assert::assertContains('testing', $this->response->getBody()->getContents());
} else {
PHPUnit_Framework_Assert::assertNotContains('testing', $this->response->getBody()->getContents());
}
}
示例6: handleError
/**
* @throws HttpException
*/
protected function handleError()
{
$body = (string) $this->response->getBody();
$code = (int) $this->response->getStatusCode();
$content = json_decode($body);
throw new HttpException(isset($content->message) ? $content->message : 'Request not processed.', $code);
}
示例7: decode
public function decode(ResponseInterface $raw, $totalTime = 0)
{
$executionTime = $callTime = 0;
if ($raw->hasHeader('X-Execution-Time')) {
$executionTime = $raw->getHeader('X-Execution-Time');
}
if ($raw->hasHeader('X-Call-Time')) {
$callTime = $raw->getHeader('X-Call-Time');
}
$body = '';
try {
$body = (string) $raw->getBody();
$result = $this->_getDecoder()->decode($body, self::FORMAT, $this->_getDecodeContext());
} catch (\Exception $e) {
if (!empty($body)) {
$body = ' (' . $body . ')';
}
error_log("Invalid API Response: " . $body);
throw new InvalidApiResponseException("Unable to decode raw api response.", 500, $e);
}
if (!property_exists($result, 'type') || !property_exists($result, 'status') || !property_exists($result, 'result') || !property_exists($result->status, 'message') || !property_exists($result->status, 'code')) {
error_log("Invalid API Result: " . json_encode($result));
throw new InvalidApiResponseException("Invalid api result", 500);
}
if ($executionTime === 0) {
$executionTime = $totalTime;
}
if ($callTime === 0) {
$callTime = $executionTime;
}
return ResponseBuilder::create(ApiCallData::create($result->type, $result->result, $result->status->code, $result->status->message, (double) str_replace([',', 'ms'], '', $totalTime), (double) str_replace([',', 'ms'], '', $executionTime), (double) str_replace([',', 'ms'], '', $callTime)));
}
示例8: 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;
}
示例9: requestTagsForUser
/**
* Returns all tags for a given user
*
* @param string $user
* @return array
*/
private function requestTagsForUser($user)
{
try {
$request = $this->client->createRequest('PROPFIND', $this->baseUrl . '/remote.php/dav/systemtags/', ['body' => '<?xml version="1.0"?>
<d:propfind xmlns:d="DAV:" xmlns:oc="http://owncloud.org/ns">
<d:prop>
<oc:id />
<oc:display-name />
<oc:user-visible />
<oc:user-assignable />
</d:prop>
</d:propfind>', 'auth' => [$user, $this->getPasswordForUser($user)], 'headers' => ['Content-Type' => 'application/json']]);
$this->response = $this->client->send($request);
} catch (\GuzzleHttp\Exception\ClientException $e) {
$this->response = $e->getResponse();
}
$tags = [];
$service = new Sabre\Xml\Service();
$parsed = $service->parse($this->response->getBody()->getContents());
foreach ($parsed as $entry) {
$singleEntry = $entry['value'][1]['value'][0]['value'];
if (empty($singleEntry[0]['value'])) {
continue;
}
$tags[$singleEntry[0]['value']] = ['display-name' => $singleEntry[1]['value'], 'user-visible' => $singleEntry[2]['value'], 'user-assignable' => $singleEntry[3]['value']];
}
return $tags;
}
示例10:
function it_should_make_post_request(ResponseInterface $responce)
{
$responce->getHeader("Content-Type")->willReturn('blablaxmlblabla');
$responce->getBody()->willReturn('<xml></xml>');
$this->client->post(Argument::any(), Argument::any())->willReturn($responce);
$responce->xml()->willReturn(new \SimpleXMLElement('<xml></xml>'));
$this->post(Argument::type('string'))->shouldBeAnInstanceOf('Bxav\\Component\\ResellerClub\\Model\\XmlResponse');
}
示例11: __construct
/**
* @param ResponseInterface $response raw json response
* @param bool $throw Should throw API errors as exceptions
*
* @throws \Exception
*/
public function __construct(ResponseInterface $response = null, $throw = true)
{
if ($response !== null) {
$this->_executionTime = $response->getHeader('X-Execution-Time');
$this->_callTime = $response->getHeader('X-Call-Time');
$this->readJson((string) $response->getBody(), $throw);
}
}
示例12:
function it_should_return_a_service_response_when_verifying_an_ipn_message(Client $httpClient, Message $message, ResponseInterface $response)
{
$response->getBody()->willReturn('foo');
$httpClient->post(Argument::type('string'), Argument::type('array'))->willReturn($response);
$message->getAll()->willReturn(['foo' => 'bar']);
$response = $this->verifyIpnMessage($message);
$response->shouldHaveType('Mdb\\PayPal\\Ipn\\ServiceResponse');
$response->getBody()->shouldReturn('foo');
}
示例13: getRawResponseBody
/**
* @param ResponseInterface|Response6Interface $response
*
* @return string
*
* @throws CommunicationException
*/
public static function getRawResponseBody($response)
{
$statusCode = $response->getStatusCode();
$rawResponse = $response->getBody()->getContents();
if ($statusCode != 200) {
throw CommunicationException::createFromHTTPResponse($statusCode, $rawResponse);
}
return $rawResponse;
}
示例14: handleError
/**
* @throws ApiException
*/
protected function handleError($getCode = false)
{
$code = (int) $this->response->getStatusCode();
if ($getCode) {
return $code;
}
$content = (string) $this->response->getBody();
$this->reportError($code, $content);
}
示例15: processResponse
/**
* @param \GuzzleHttp\Message\ResponseInterface $response
* @return Language[]
*/
public function processResponse(\GuzzleHttp\Message\ResponseInterface $response)
{
$xml = simplexml_load_string($response->getBody()->getContents());
$languages = [];
foreach ($xml->string as $language) {
$languages[] = new Language($language);
}
return $languages;
}