本文整理汇总了PHP中Guzzle\Http\Message\Response::getContentType方法的典型用法代码示例。如果您正苦于以下问题:PHP Response::getContentType方法的具体用法?PHP Response::getContentType怎么用?PHP Response::getContentType使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Guzzle\Http\Message\Response
的用法示例。
在下文中一共展示了Response::getContentType方法的11个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: 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);
}
示例2: __construct
public function __construct(\Guzzle\Http\Message\Response $response, $type)
{
$this->_response = $response;
// if json returned - parse and fill structure
$contentType = $this->_response->getContentType();
if (strpos($contentType, ';')) {
list($contentType, ) = explode(';', $contentType);
}
if ($contentType == 'application/json') {
$this->_structure = new $type($this->_response->json());
} else {
throw new \Exception('Structure parser for content type "' . $this->_response->getContentType() . '" not implemented');
}
}
示例3: 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();
}
示例4: createResponse
/**
* Reads response meta tags to guess content-type charset.
*/
protected function createResponse(GuzzleResponse $response)
{
$body = $response->getBody(true);
$contentType = $response->getContentType();
if (!$contentType || false === strpos($contentType, 'charset=')) {
if (preg_match('/\\<meta[^\\>]+charset *= *["\']?([a-zA-Z\\-0-9]+)/i', $body, $matches)) {
$contentType .= ';charset=' . $matches[1];
}
}
$response->setHeader('Content-Type', $contentType);
return parent::createResponse($response);
}
示例5: createResponse
/**
* Reads response meta tags to guess content-type charset.
*/
protected function createResponse(GuzzleResponse $response)
{
$body = $response->getBody(true);
$statusCode = $response->getStatusCode();
$headers = $response->getHeaders()->getAll();
$contentType = $response->getContentType();
if (!$contentType || false === strpos($contentType, 'charset=')) {
if (preg_match('/\\<meta[^\\>]+charset *= *["\']?([a-zA-Z\\-0-9]+)/i', $body, $matches)) {
$contentType .= ';charset=' . $matches[1];
}
}
$headers['Content-Type'] = $contentType;
return new Response($body, $statusCode, $headers);
}
示例6: factory
/**
* Factory method to create a new response exception based on the response code.
*
* @param RequestInterface $request Request
* @param Response $response Response received
* @param string $label
*
* @return BadResponseException
*/
public static function factory(RequestInterface $request, Response $response, $label = null)
{
if (!$label) {
if ($response->isClientError()) {
$label = 'Client error response';
} elseif ($response->isServerError()) {
$label = 'Server error response';
} else {
$label = 'Unsuccessful response';
}
}
$message = $label . PHP_EOL . implode(PHP_EOL, array('[status code] ' . $response->getStatusCode(), '[reason phrase] ' . $response->getReasonPhrase(), '[url] ' . $request->getUrl(), '[content type] ' . $response->getContentType(), '[response body] ' . $response->getBody(true)));
$result = new static($message);
$result->setResponse($response);
$result->setRequest($request);
return $result;
}
示例7: isHttpErrorFatal
/**
* @return bool
*/
private function isHttpErrorFatal(Response $response, $expectedResponseContentType)
{
return $response->isSuccessful() && $response->getContentType() != $expectedResponseContentType;
}
示例8: parseResponse
private function parseResponse(Response $response, $path)
{
$statusCode = $response->getStatusCode();
if ($statusCode !== 200) {
throw new UnexpectedValueException(sprintf('Expected status code 200 from "%s", got %d', $path, $statusCode));
}
$contentType = $response->hasHeader('content-type') ? $response->getContentType() : '';
if (substr($contentType, 0, 10) !== 'text/plain') {
throw new UnexpectedValueException(sprintf('Expected content type "text/plain" from "%s", got "%s"', $path, $contentType));
}
return $this->parseRequestFromResponse($response, $path);
}
示例9: handleResponse
/**
* @param RequestInterface $request
* @param Response $response
* @return \Guzzle\Http\EntityBodyInterface|mixed|string
*/
public function handleResponse(RequestInterface $request, Response $response)
{
$body = $response->getBody(true);
switch ($response->getContentType()) {
case "application/json":
$body = json_decode($body, true);
break;
}
return [$response->getStatusCode(), $body];
}
示例10: getContentType
/**
* {@inheritdoc}
*/
public function getContentType()
{
return $this->response->getContentType();
}
示例11: pushLinksForResponse
function pushLinksForResponse(Guzzle\Http\Message\Response $resp)
{
$self = null;
$hubs = [];
$linkHeader = $resp->getHeader('link');
if ($linkHeader instanceof Guzzle\Http\Message\Header\Link) {
$links = $linkHeader->getLinks();
foreach ($links as $link) {
if (strpos(" {$link['rel']} ", ' self ') !== false) {
$self = $link['url'];
}
if (strpos(" {$link['rel']} ", ' hub ') !== false) {
$hubs[] = $link['url'];
}
}
}
if (strpos($resp->getContentType(), 'html') !== false) {
$mf = Mf2\parse($resp->getBody(true), $resp->getEffectiveUrl());
if (!empty($mf['rels']['hub'])) {
$hubs = array_merge($hubs, $mf['rels']['hub']);
}
if (!empty($mf['rels']['self']) and $self === null) {
$self = $mf['rels']['self'][0];
}
}
return ['self' => $self, 'hub' => $hubs];
}