本文整理汇总了PHP中GuzzleHttp\Psr7\Response::getBody方法的典型用法代码示例。如果您正苦于以下问题:PHP Response::getBody方法的具体用法?PHP Response::getBody怎么用?PHP Response::getBody使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类GuzzleHttp\Psr7\Response
的用法示例。
在下文中一共展示了Response::getBody方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getEvents
/**
* Returns generator that yields new event when it's available on stream.
*
* @return Event[]
*/
public function getEvents()
{
$buffer = '';
$body = $this->response->getBody();
while (true) {
// if server close connection - try to reconnect
if ($body->eof()) {
// wait retry period before reconnection
sleep($this->retry / 1000);
$this->connect();
// clear buffer since there is no sense in partial message
$buffer = '';
}
$buffer .= $body->read(1);
if (preg_match(self::END_OF_MESSAGE, $buffer)) {
$parts = preg_split(self::END_OF_MESSAGE, $buffer, 2);
$rawMessage = $parts[0];
$remaining = $parts[1];
$buffer = $remaining;
$event = Event::parse($rawMessage);
// if message contains id set it to last received message id
if ($event->getId()) {
$this->lastId = $event->getId();
}
// take into account server request for reconnection delay
if ($event->getRetry()) {
$this->retry = $event->getRetry();
}
(yield $event);
}
}
}
示例2: theResponseIsJson
/**
* @Then the response is JSON
*/
public function theResponseIsJson()
{
$this->responseData = json_decode($this->response->getBody());
$expectedType = 'object';
$actualObject = $this->responseData;
PHPUnit::assertInternalType($expectedType, $actualObject);
}
示例3: __construct
/**
* Initialize
*
* @param HttpResponse $response
*/
public function __construct(HttpResponse $response)
{
$this->response = $response;
if ($this->isSuccessful()) {
$this->messages = new MessageGroup(json_decode($this->response->getBody(), true));
}
}
示例4: __construct
public function __construct(\GuzzleHttp\Psr7\Response $response)
{
$json = false;
$data = $response->getBody();
$this->rawData = $data;
$this->response = $response;
if ($response->hasHeader('Content-Type')) {
// Let's see if it is JSON
$contentType = $response->getHeader('Content-Type');
if (strstr($contentType[0], 'json')) {
$json = true;
$data = json_decode($data);
}
}
if (!$json) {
// We can do another test here
$decoded = json_decode($response->getBody());
if ($decoded) {
$json = true;
$data = $decoded;
}
}
$this->setData($data);
$this->setIsJson($json);
}
示例5: contents
/**
* Get the response body contents.
*
* @return string
* The original body contents.
*/
public function contents()
{
$body = $this->response->getBody();
if (!$body) {
return null;
}
return $body->getContents();
}
示例6: correctIpReturnsDecodedInfo
/**
* @test
*/
public function correctIpReturnsDecodedInfo()
{
$expected = ['foo' => 'bar', 'baz' => 'foo'];
$response = new Response();
$response->getBody()->write(json_encode($expected));
$response->getBody()->rewind();
$this->client->get('http://freegeoip.net/json/1.2.3.4')->willReturn($response)->shouldBeCalledTimes(1);
$this->assertEquals($expected, $this->ipResolver->resolveIpLocation('1.2.3.4'));
}
示例7: deduce
/**
* @return ErrorResponse|SuccessResponse
*/
public function deduce()
{
/* @var array $response */
$response = (array) json_decode($this->response->getBody()->getContents());
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());
}
示例8: __construct
/**
* Response constructor
*
* @param HttpResponse $httpResponse
* @return self
*/
public function __construct(HttpResponse $httpResponse)
{
$header = $httpResponse->getHeader('Content-Type');
if (!is_array($header) && stripos($header, 'text/javascript') !== false) {
$this->_response = $this->processJson($httpResponse->getBody());
} else {
$this->_response = $this->processXml($httpResponse->getBody());
}
}
示例9: getResponse
private function getResponse(HttpResponse $httpResponse)
{
$response = new Response();
if ($httpResponse->getBody()) {
$resp = (string) $httpResponse->getBody();
$decoded = json_decode($resp, true);
$response->setBody($decoded);
}
return $response;
}
示例10: parse
protected function parse(Response $response, $type = 'json')
{
switch ($type) {
case 'json':
return json_decode((string) $response->getBody(), true);
case 'xml':
return simplexml_load_file((string) $response->getBody());
default:
throw new Exception("Invalid repsonse type");
}
}
示例11: buildResponse
/**
* Create an array of the response object
*
* @param Response $response
* @return array
*/
private function buildResponse(Response $response)
{
$this->response['body'] = $response->getBody()->getContents();
$this->response['size'] = $response->getBody()->getSize();
$this->response['status_code'] = $response->getStatusCode();
//Build the response headers
$headers = $response->getHeaders();
foreach ($headers as $name => $value) {
$this->response['headers'][$name] = $value;
}
return $this->response;
}
示例12: parseResponse
/**
* @param Response $response
* @return array Parsed JSON result
* @throws JotihuntApiException
*/
public function parseResponse(Response $response)
{
try {
$resultArray = json_decode($response->getBody()->getContents(), true);
if (!is_array($resultArray)) {
throw new JotihuntApiException('Jotihunt error: Unexpected result: ' . $response->getBody()->getContents());
}
if (array_key_exists('error', $resultArray)) {
throw new JotihuntApiException('Jotihunt error: ' . $resultArray['error']);
}
return $resultArray;
} catch (\RuntimeException $e) {
throw new JotihuntApiException('Jotihunt error: ' . $e->getMessage());
}
}
示例13: toXML
/**
* Returns response in XML format
* @return \SimpleXMLElement
* @throws Exception
*/
public function toXML()
{
// Allow Internal Error Processing
libxml_use_internal_errors(true);
// Load Response to XML
$xml = simplexml_load_string($this->response->getBody()->getContents());
// Check for Errors
$xml_error = libxml_get_last_error();
if ($xml_error !== false) {
// Clear errors for next request
libxml_clear_errors();
throw new Exception($xml_error->message, $xml_error->code);
}
return $xml;
}
示例14: getJsonResponse
public static function getJsonResponse(GuzzleHttp\Psr7\Response $res)
{
if ($res->getStatusCode() == 200) {
return json_decode($res->getBody()->getContents(), true);
}
return [];
}
示例15: __construct
/**
* PlayerCommandsResponse constructor.
* @param Response $response
*/
public function __construct(Response $response)
{
$object = json_decode($response->getBody());
foreach ($object->commands as $command) {
$this->commands[] = new Command($command);
}
}