本文整理汇总了PHP中GuzzleHttp\Message\Response类的典型用法代码示例。如果您正苦于以下问题:PHP Response类的具体用法?PHP Response怎么用?PHP Response使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Response类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: parseQuery
/**
* @param Response $response
* @return array
*/
public static function parseQuery(Response $response)
{
$responseBody = $response->getBody();
$params = [];
parse_str($responseBody, $params);
return $params;
}
示例2: createResponse
/**
* Taken from Mink\BrowserKitDriver
*
* @param Response $response
*
* @return \Symfony\Component\BrowserKit\Response
*/
protected function createResponse(Response $response)
{
$contentType = $response->getHeader('Content-Type');
$matches = null;
if (!$contentType or strpos($contentType, 'charset=') === false) {
$body = $response->getBody(true);
if (preg_match('/\\<meta[^\\>]+charset *= *["\']?([a-zA-Z\\-0-9]+)/i', $body, $matches)) {
$contentType .= ';charset=' . $matches[1];
}
$response->setHeader('Content-Type', $contentType);
}
$headers = $response->getHeaders();
$status = $response->getStatusCode();
$matchesMeta = null;
$matchesHeader = null;
$isMetaMatch = preg_match('/\\<meta[^\\>]+http-equiv="refresh" content="(\\d*)\\s*;?\\s*url=(.*?)"/i', $response->getBody(true), $matchesMeta);
$isHeaderMatch = preg_match('~(\\d*);?url=(.*)~', (string) $response->getHeader('Refresh'), $matchesHeader);
$matches = $isMetaMatch ? $matchesMeta : $matchesHeader;
if (!empty($matches) && (empty($matches[1]) || $matches[1] < $this->refreshMaxInterval)) {
$uri = $this->getAbsoluteUri($matches[2]);
$partsUri = parse_url($uri);
$partsCur = parse_url($this->getHistory()->current()->getUri());
foreach ($partsCur as $key => $part) {
if ($key === 'fragment') {
continue;
}
if (!isset($partsUri[$key]) || $partsUri[$key] !== $part) {
$status = 302;
$headers['Location'] = $uri;
break;
}
}
}
return new BrowserKitResponse($response->getBody(), $status, $headers);
}
示例3: formatProvider
public function formatProvider()
{
$request = new Request('PUT', '/', ['x-test' => 'abc'], Stream::factory('foo'));
$response = new Response(200, ['X-Baz' => 'Bar'], Stream::factory('baz'));
$err = new RequestException('Test', $request, $response);
return [['{request}', [$request], (string) $request], ['{response}', [$request, $response], (string) $response], ['{request} {response}', [$request, $response], $request . ' ' . $response], ['{request} {response}', [$request], $request . ' '], ['{req_headers}', [$request], "PUT / HTTP/1.1\r\nx-test: abc"], ['{res_headers}', [$request, $response], "HTTP/1.1 200 OK\r\nX-Baz: Bar"], ['{res_headers}', [$request], 'NULL'], ['{req_body}', [$request], 'foo'], ['{res_body}', [$request, $response], 'baz'], ['{res_body}', [$request], 'NULL'], ['{method}', [$request], $request->getMethod()], ['{url}', [$request], $request->getUrl()], ['{resource}', [$request], $request->getResource()], ['{req_version}', [$request], $request->getProtocolVersion()], ['{res_version}', [$request, $response], $response->getProtocolVersion()], ['{res_version}', [$request], 'NULL'], ['{host}', [$request], $request->getHost()], ['{hostname}', [$request, $response], gethostname()], ['{hostname}{hostname}', [$request, $response], gethostname() . gethostname()], ['{code}', [$request, $response], $response->getStatusCode()], ['{code}', [$request], 'NULL'], ['{phrase}', [$request, $response], $response->getReasonPhrase()], ['{phrase}', [$request], 'NULL'], ['{error}', [$request, $response, $err], 'Test'], ['{error}', [$request], 'NULL'], ['{req_header_x-test}', [$request], 'abc'], ['{req_header_x-not}', [$request], ''], ['{res_header_X-Baz}', [$request, $response], 'Bar'], ['{res_header_x-not}', [$request, $response], ''], ['{res_header_X-Baz}', [$request], 'NULL']];
}
示例4: __construct
/**
* Constructor
*
* @param GuzzleResponse $response
*/
public function __construct(GuzzleResponse $response)
{
$headers = $response->getHeaders();
//-- this is a hack on my part and I'm terribly sorry it exists
//-- but deal with it.
foreach ($headers as $header => $value) {
$this->headers[$header] = implode(',', array_values((array) $value));
}
$this->url = $response->getEffectiveUrl();
$url_parts = explode('?', $this->url);
if (isset($url_parts[1])) {
$query = explode('&', $url_parts[1]);
foreach ($query as $params) {
$kv = explode('=', $params);
$this->params[$kv[0]] = isset($kv[1]) ? $kv[1] : '';
}
}
// $this->params = $response->request_parameters;
// $this->method = $response->request_method;
$this->json = $response->json();
// $json = json_decode($this->json, TRUE);
$this->data = isset($this->json['data']) ? $this->json['data'] : [];
$this->meta = isset($this->json['meta']) ? $this->json['meta'] : [];
if (isset($this->json['code']) && $this->json['code'] !== 200) {
$this->meta = $this->json;
}
$this->pagination = isset($this->json['pagination']) ? $this->json['pagination'] : [];
$this->user = isset($this->json['user']) ? $this->json['user'] : [];
$this->access_token = isset($this->json['access_token']) ? $this->json['access_token'] : NULL;
$this->limit = isset($this->headers[self::RATE_LIMIT_HEADER]) ? $this->headers[self::RATE_LIMIT_HEADER] : 0;
$this->remaining = isset($this->headers[self::RATE_LIMIT_REMAINING_HEADER]) ? $this->headers[self::RATE_LIMIT_REMAINING_HEADER] : 0;
}
示例5: handleResponse
protected function handleResponse(Response $response)
{
if ($response->getStatusCode() == 200) {
return $response->getBody()->getContents();
}
throw new ApiException('Request failed, received the following status: ' . $response->getStatusCode() . ' Body: ' . $response->getBody()->getContents());
}
示例6: getMessages
protected function getMessages(Response $response)
{
$body = (string) $response->getBody();
if (empty($body)) {
throw new InvalidResponseException('There was no messages returned from the server at: ' . $this->uri);
}
$messages = array();
$messageCounter = 0;
while ($body) {
$lineLengthHex = substr($body, 0, 4);
if (strlen($lineLengthHex) != 4) {
throw new InvalidResponseException('A corrupt package was received from the server. Uri: ' . $this->uri);
}
if ($lineLengthHex == '0000') {
$messageCounter++;
$body = substr_replace($body, '', 0, 4);
continue;
}
$lineLength = hexdec($lineLengthHex);
$line = substr($body, 4, $lineLength - 4);
$body = substr_replace($body, '', 0, $lineLength);
$messages[$messageCounter][] = trim($line);
}
return $messages;
}
示例7: debug
function debug(\GuzzleHttp\Message\Response $response)
{
$this->dump_respuesta = 'status: ' . $response->getStatusCode() . "<br/>body: <br/>" . $response->getBody();
//un string encodeado utf-8
$this->dump_url = $response->getEffectiveUrl();
//un string encodeado utf-8
}
示例8: getResponse
protected function getResponse($body)
{
$stream = Stream::factory($body);
$response = new Response(200);
$response->setBody($stream);
return $response;
}
示例9: makeResponse
/**
* Create a Response object from a stub.
*
* @param $stub
* @return \GuzzleHttp\Message\Response
*/
private function makeResponse($stub)
{
$response = new Response(200);
$response->setHeader('Content-Type', 'application/json');
$responseBody = Stream::factory(fopen('./tests/Destiny/stubs/' . $stub . '.txt', 'r+'));
$response->setBody($responseBody);
return $response;
}
示例10: getMockWithCode
private function getMockWithCode($code, $body = null)
{
$response = new Response($code, array());
if (isset($body) === true) {
$response->setBody(Stream::factory(json_encode($body)));
}
$this->mockResponse = new Mock(array($response));
}
示例11: __construct
/**
* Response constructor
*
* @param HttpResponse $httpResponse
* @return self
*/
public function __construct(HttpResponse $httpResponse)
{
if (stripos($httpResponse->getHeader('Content-Type'), 'text/javascript') !== false) {
$this->_response = $this->processJson($httpResponse->getBody());
} else {
$this->_response = $this->processXml($httpResponse->getBody());
}
}
示例12: addClientMock
/**
* Adds a mock repsonse to the response queue
*
* @param \GuzzleHttp\Stream\Stream $data Stream data object
* @param int $response_code desired response code
*/
protected function addClientMock($data, $response_code = 200)
{
//create a response with the data and response code
$api_response = new Response($response_code);
$api_response->setBody($data);
$mock_response = $this->getMockObject();
$mock_response->addResponse($api_response);
}
示例13: wrapResponse
private static function wrapResponse(Response $response)
{
$payload = $response->json(["object" => true]);
// strip the envelope
if (isset($payload->data) && isset($payload->meta)) {
return new static($payload->data);
}
return new static($payload);
}
示例14: saveResponseToFile
protected function saveResponseToFile(Response &$response)
{
$fileName = __DIR__ . '\\json\\' . sizeof($this->fileHandles) . '.json';
$reponseSavedToFile = file_put_contents($fileName, $response->getBody());
if ($reponseSavedToFile != false) {
$this->fileHandles[] = $fileName;
}
unset($response);
}
示例15: validateBody
public function validateBody(Response $response, Spec\Response $responseSpec)
{
$actualBody = (string) $response->getBody();
$actualBodyData = json_decode($actualBody, true);
if ($actualBodyData !== $responseSpec->getBody()) {
$message = sprintf("\t\t<error>JSON in body of the response is invalid, actual:</error>\n%s\n\t\t<error>Expected:</error>\n%s", $this->getOutput()->formatArray($actualBodyData, 3), $this->getOutput()->formatArray($responseSpec->getBody(), 3));
$this->addViolation($message);
}
}