本文整理汇总了PHP中Guzzle\Http\Message\Response::getBody方法的典型用法代码示例。如果您正苦于以下问题:PHP Response::getBody方法的具体用法?PHP Response::getBody怎么用?PHP Response::getBody使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Guzzle\Http\Message\Response
的用法示例。
在下文中一共展示了Response::getBody方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: cache
public function cache(RequestInterface $request, Response $response)
{
$currentTime = time();
$ttl = $request->getParams()->get('cache.override_ttl') ?: $response->getMaxAge() ?: $this->defaultTtl;
if ($cacheControl = $response->getHeader('Cache-Control')) {
$stale = $cacheControl->getDirective('stale-if-error');
$ttl += $stale == true ? $ttl : $stale;
}
// Determine which manifest key should be used
$key = $this->getCacheKey($request);
$persistedRequest = $this->persistHeaders($request);
$entries = array();
if ($manifest = $this->cache->fetch($key)) {
// Determine which cache entries should still be in the cache
$vary = $response->getVary();
foreach (unserialize($manifest) as $entry) {
// Check if the entry is expired
if ($entry[4] < $currentTime) {
continue;
}
$entry[1]['vary'] = isset($entry[1]['vary']) ? $entry[1]['vary'] : '';
if ($vary != $entry[1]['vary'] || !$this->requestsMatch($vary, $entry[0], $persistedRequest)) {
$entries[] = $entry;
}
}
}
// Persist the response body if needed
$bodyDigest = null;
if ($response->getBody() && $response->getBody()->getContentLength() > 0) {
$bodyDigest = $this->getBodyKey($request->getUrl(), $response->getBody());
$this->cache->save($bodyDigest, (string) $response->getBody(), $ttl);
}
array_unshift($entries, array($persistedRequest, $this->persistHeaders($response), $response->getStatusCode(), $bodyDigest, $currentTime + $ttl));
$this->cache->save($key, serialize($entries));
}
示例2: getDocument
/**
* @return \DOMDocument
*/
public function getDocument()
{
$request = $this->client->get($this->url);
$this->response = $request->send();
$this->setHtml($this->response->getBody(true));
return parent::getDocument();
}
示例3: request
/**
* Returns object with properties int:status object:body
* @param string $method
* @param string $path
* @param array $query
* @param bool $doAuth
* @throws \InvalidArgumentException
* @throws \Exception
* @return \stdClass
*/
public function request($method, $path, $query = array(), $doAuth = false)
{
$this->userAgent = 'Rocker REST Client v' . Server::VERSION;
$method = strtolower($method);
$request = $this->initiateRequest($method, $path, $query);
if ($doAuth) {
$this->addAuthHeader($request);
}
try {
$this->lastResponse = $request->send();
} catch (\Guzzle\Http\Exception\ClientErrorResponseException $e) {
$this->lastResponse = $e->getResponse();
if ($this->lastResponse->getStatusCode() == 401 && !$doAuth && !empty($this->user)) {
trigger_error('Doing unauthenticated requests to an URI that requires authentication (' . $path . ')', E_WARNING);
return $this->request($method, $path, $query, true);
}
}
if ($this->lastResponse->getStatusCode() == 400) {
throw new ClientException($this->lastResponse, 400);
}
if ($this->lastResponse->getStatusCode() == 204) {
return (object) array('status' => 204, 'body' => array());
}
if (strpos($this->lastResponse->getContentType(), 'json') === false) {
throw new ClientException($this->lastResponse, ClientException::ERR_UNEXPECTED_CONTENT_TYPE, 'Server responded with unexpected content type (' . $this->lastResponse->getContentType() . ')');
}
$str = (string) $this->lastResponse->getBody();
$body = json_decode($str);
return (object) array('status' => $this->lastResponse->getStatusCode(), 'headers' => $this->headerCollectionToArray($this->lastResponse->getHeaders()), 'body' => $body);
}
示例4: getBodyAsString
/**
* {@inheritdoc}
*/
public function getBodyAsString()
{
try {
$result = $this->response->getBody(true);
} catch (\Exception $exception) {
throw GuzzleRestException::createFromException($exception);
}
return $result;
}
示例5: getContent
private function getContent(Response $response)
{
if ($response->isContentType('xml')) {
return $response->xml();
} elseif ($response->isContentType('audio')) {
return $response->getBody()->getStream();
} else {
return $response->getBody();
}
}
示例6: getResponseBody
public function getResponseBody()
{
$bodyObject = $this->response->getBody();
$bodyObject->rewind();
$length = $bodyObject->getContentLength();
if ($length === false || $length <= 0) {
return "";
}
return $bodyObject->read($length);
}
示例7: parseRequestFromResponse
/**
* @param Response $response
* @param string $path
* @throws UnexpectedValueException
* @return UnifiedRequest
*/
private function parseRequestFromResponse(Response $response, $path)
{
try {
$requestInfo = Util::deserialize($response->getBody());
} catch (UnexpectedValueException $e) {
throw new UnexpectedValueException(sprintf('Cannot deserialize response from "%s": "%s"', $path, $response->getBody()), null, $e);
}
$request = RequestFactory::getInstance()->fromMessage($requestInfo['request']);
$params = $this->configureRequest($request, $requestInfo['server'], isset($requestInfo['enclosure']) ? $requestInfo['enclosure'] : []);
return new UnifiedRequest($request, $params);
}
示例8: parseRequestFromResponse
/**
* @param Response $response
* @param string $path
* @throws UnexpectedValueException
* @return UnifiedRequest
*/
private function parseRequestFromResponse(Response $response, $path)
{
// @codingStandardsIgnoreStart
$requestInfo = @unserialize($response->getBody());
// @codingStandardsIgnoreEnd
if ($requestInfo === false) {
throw new UnexpectedValueException(sprintf('Cannot deserialize response from "%s": "%s"', $path, $response->getBody()));
}
$request = RequestFactory::getInstance()->fromMessage($requestInfo['request']);
$params = $this->configureRequest($request, $requestInfo['server']);
return new UnifiedRequest($request, $params);
}
示例9: handleError
/**
* @throws HttpException
*/
protected function handleError()
{
$body = (string) $this->response->getBody(true);
$code = (int) $this->response->getStatusCode();
$content = json_decode($body);
throw new HttpException(isset($content->message) ? $content->message : 'Request not processed.', $code);
}
示例10: __construct
/**
* @param UriInterface $uri
* @param Response $response
*/
public function __construct(UriInterface $uri, Response $response)
{
$this->uri = $uri;
$this->response = $response;
// we store the response manually, because otherwise it will not get serialized. It is a php://temp stream
$this->body = $response->getBody(true);
}
示例11: testProperlyValidatesWhenUsingContentEncoding
public function testProperlyValidatesWhenUsingContentEncoding()
{
$plugin = new Md5ValidatorPlugin(true);
$request = RequestFactory::getInstance()->create('GET', 'http://www.test.com/');
$request->getEventDispatcher()->addSubscriber($plugin);
// Content-MD5 is the MD5 hash of the canonical content after all
// content-encoding has been applied. Because cURL will automatically
// decompress entity bodies, we need to re-compress it to calculate.
$body = EntityBody::factory('abc');
$body->compress();
$hash = $body->getContentMd5();
$body->uncompress();
$response = new Response(200, array('Content-MD5' => $hash, 'Content-Encoding' => 'gzip'), 'abc');
$request->dispatch('request.complete', array('response' => $response));
$this->assertEquals('abc', $response->getBody(true));
// Try again with an unknown encoding
$response = new Response(200, array('Content-MD5' => $hash, 'Content-Encoding' => 'foobar'), 'abc');
$request->dispatch('request.complete', array('response' => $response));
// Try again with compress
$body->compress('bzip2.compress');
$response = new Response(200, array('Content-MD5' => $body->getContentMd5(), 'Content-Encoding' => 'compress'), 'abc');
$request->dispatch('request.complete', array('response' => $response));
// Try again with encoding and disabled content-encoding checks
$request->getEventDispatcher()->removeSubscriber($plugin);
$plugin = new Md5ValidatorPlugin(false);
$request->getEventDispatcher()->addSubscriber($plugin);
$request->dispatch('request.complete', array('response' => $response));
}
示例12: onRequestError
/**
* request error
* @param \Guzzle\Common\Event $event
*/
public function onRequestError(Event $event)
{
$this->request = $event->offsetGet('request');
$this->response = $event->offsetGet('response');
$body = $this->response->getBody(true);
switch ($this->response->getStatusCode()) {
case 400:
$this->error400($body);
break;
case 520:
$this->error520($body);
break;
default:
$this->commonError($body);
break;
}
}
示例13: 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();
}
示例14: isSuccess
/**
* Should return if sending the data was successful
*
* @return bool
*/
public function isSuccess()
{
$statuscode = $this->response->getStatusCode();
if (!in_array($statuscode, ['200', '204'])) {
throw new \Exception('HTTP Code ' . $statuscode . ' ' . $this->response->getBody());
}
return true;
}
示例15: getResponseBody
/**
* {@inheritdoc}
*/
public function getResponseBody()
{
if (null === $this->response) {
return array();
}
$body = json_decode($this->response->getBody(true), true);
return is_array($body) ? $body : array();
}