本文整理汇总了PHP中Guzzle\Http\Message\Response类的典型用法代码示例。如果您正苦于以下问题:PHP Response类的具体用法?PHP Response怎么用?PHP Response使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Response类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: getResponse
/**
* Get the response of the query.
* Will be the json decoded data if success, else the error message.
*
* @return array|string
*/
public function getResponse()
{
if (false === $this->response->isSuccessful()) {
return $this->response->getMessage();
}
return $this->response->json();
}
示例2: dispatch
public function dispatch($request, $connection)
{
$response = new Response(200);
$path = $request->getPath();
if ($this->match($path)) {
echo "Found match for path: {$path}" . PHP_EOL;
if (is_callable($this->routes[$path])) {
call_user_func_array($this->routes[$path], array($request, $response, $connection));
} else {
throw new \Exception('Invalid route definition.');
}
return;
}
if (is_null($this->fileHandler)) {
throw new \Exception('No file handler configured');
}
if (false === $this->fileHandler->canHandleRequest($request)) {
$response->setStatus(404);
$response->setBody('File not found');
$connection->send($response);
$connection->close();
return;
}
$this->fileHandler->handleRequest($request, $response, $connection);
}
示例3: getDelay
/**
* {@inheritdoc}
*/
protected function getDelay($retries, RequestInterface $request, Response $response = null, HttpException $e = null)
{
if ($response && $response->isClientError()) {
$parts = $this->exceptionParser->parse($request, $response);
return isset(self::$throttlingExceptions[$parts['code']]) ? true : null;
}
}
示例4: 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));
}
示例5: receiveResponseHeader
/**
* Receive a response header from curl
*
* @param resource $curl Curl handle
* @param string $header Received header
*
* @return int
*/
public function receiveResponseHeader($curl, $header)
{
static $normalize = array("\r", "\n");
$length = strlen($header);
$header = str_replace($normalize, '', $header);
if (strpos($header, 'HTTP/') === 0) {
$startLine = explode(' ', $header, 3);
$code = $startLine[1];
$status = isset($startLine[2]) ? $startLine[2] : '';
// Only download the body of the response to the specified response
// body when a successful response is received.
if ($code >= 200 && $code < 300) {
$body = $this->request->getResponseBody();
} else {
$body = EntityBody::factory();
}
$response = new Response($code, null, $body);
$response->setStatus($code, $status);
$this->request->startResponse($response);
$this->request->dispatch('request.receive.status_line', array('request' => $this, 'line' => $header, 'status_code' => $code, 'reason_phrase' => $status));
} elseif ($pos = strpos($header, ':')) {
$this->request->getResponse()->addHeader(trim(substr($header, 0, $pos)), trim(substr($header, $pos + 1)));
}
return $length;
}
示例6: testRetrieving
public function testRetrieving()
{
$response = new Response(200);
$response->setBody(json_encode(array('status' => 1, 'complete' => 1, 'list' => array(123 => array('item_id' => 123, 'resolved_id' => 123, 'given_url' => 'http://acairns.co.uk', 'given_title' => 'Andrew Cairns', 'favorite' => 0, 'status' => 0, 'time_added' => time(), 'time_updated' => time(), 'time_read' => 0, 'time_favorited' => 0, 'sort_id' => 0, 'resolved_title' => 'Andrew Cairns', 'resolved_url' => 'http://acairns.co.uk', 'excerpt' => 'Some excerpt about something', 'is_article' => 0, 'is_index' => 0, 'has_video' => 0, 'has_image' => 0, 'word_count' => 123)))));
$this->setPocketResponse($response);
$response = $this->pockpack->retrieve();
$this->assertEquals(1, $response->status);
$this->assertEquals(1, $response->complete);
$this->assertNotEmpty($response->list);
$this->assertNotEmpty($response->list->{123});
$item = $response->list->{123};
$this->assertEquals(123, $item->item_id);
$this->assertEquals(123, $item->resolved_id);
$this->assertEquals('Andrew Cairns', $item->given_title);
$this->assertEquals('Andrew Cairns', $item->resolved_title);
$this->assertEquals('http://acairns.co.uk', $item->given_url);
$this->assertEquals('http://acairns.co.uk', $item->resolved_url);
$this->assertEquals('Some excerpt about something', $item->excerpt);
$this->assertEquals(0, $item->is_article);
$this->assertEquals(0, $item->favorite);
$this->assertEquals(0, $item->status);
$this->assertEquals(0, $item->time_read);
$this->assertEquals(0, $item->time_favorited);
$this->assertEquals(0, $item->sort_id);
$this->assertEquals(0, $item->is_index);
$this->assertEquals(0, $item->has_video);
$this->assertEquals(0, $item->has_image);
$this->assertEquals(123, $item->word_count);
}
示例7: determineCode
/**
* Determine an exception code from the given response, exception data and
* JSON body.
*
* @param Response $response
* @param array $data
* @param array $json
*
* @return string|null
*/
private function determineCode(Response $response, array $data, array $json)
{
if (409 === $response->getStatusCode()) {
return 'DocumentAlreadyExists';
}
return $this->determineCodeFromErrors($data['errors']);
}
示例8: debug
public function debug()
{
$r = new Response();
var_dump($r->getReasonPhrase());
$req = new Request();
$req->getPath();
}
示例9: getDelay
/**
* {@inheritdoc}
*/
protected function getDelay($retries, RequestInterface $request, Response $response = null, HttpException $e = null)
{
if ($response && $response->isClientError()) {
$parts = $this->parser->parse($response);
return $parts['code'] == 'ProvisionedThroughputExceededException' || $parts['code'] == 'ThrottlingException' ? true : null;
}
}
示例10: getApiLimit
public static function getApiLimit(Response $response)
{
$remainingCalls = $response->getHeader('X-RateLimit-Remaining');
if (null !== $remainingCalls && 1 > $remainingCalls) {
throw new ApiLimitExceedException($remainingCalls);
}
}
示例11: testRateReset
/**
* Tests getRateReset method
*/
public function testRateReset()
{
$response = new Response(200);
$response->setHeader('X-Rate-Reset', '100');
$client = new GenderizeClient();
$client->setLastResponse($response);
$this->assertEquals('100', $client->getRateReset());
}
示例12: onOpen
public function onOpen(ConnectionInterface $conn, RequestInterface $request = null)
{
$this->log('open', $conn);
$response = new Response(200, array('Content-type' => 'text/javascript', 'Content-Length' => $this->getFilesize($request->getPath())));
$response->setBody($this->getContent($request->getPath()));
$conn->send((string) $response);
$conn->close();
}
示例13: getRequestTime
private function getRequestTime(GuzzleResponse $response)
{
$time = $response->getInfo('total_time');
if (null === $time) {
$time = 0;
}
return (int) ($time * 1000);
}
示例14: logResponse
/**
* @param \Guzzle\Http\Message\Response $response
* @param array $extra
*/
public function logResponse($response, $extra = array())
{
!array_key_exists("serialization_time", $extra) && ($extra["serialization_time"] = "-");
!array_key_exists("deserialization_time", $extra) && ($extra["deserialization_time"] = "-");
!array_key_exists("search_time", $extra) && ($extra["search_time"] = "-");
!array_key_exists("method", $extra) && ($extra["method"] = "-");
$this->logger->debug(self::getProcessId() . ' ' . $extra["method"] . ' ' . $response->getInfo("url") . ' ' . $response->getInfo("total_time") . ' ' . $extra["deserialization_time"] . ' ' . $extra["serialization_time"] . ' ' . $extra["search_time"]);
}
示例15: 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();
}