本文整理匯總了PHP中Psr\Http\Message\ServerRequestInterface::getMediaType方法的典型用法代碼示例。如果您正苦於以下問題:PHP ServerRequestInterface::getMediaType方法的具體用法?PHP ServerRequestInterface::getMediaType怎麽用?PHP ServerRequestInterface::getMediaType使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類Psr\Http\Message\ServerRequestInterface
的用法示例。
在下文中一共展示了ServerRequestInterface::getMediaType方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的PHP代碼示例。
示例1: __invoke
/**
* JSON middleware invokable class. It forces Content-Type on the response
* to be application/json. If the request Content-Type is not json then
* return the response with status 415 (Unsupported Media Type)
*
* @param \Psr\Http\Message\ServerRequestInterface $request PSR7 request
* @param \Psr\Http\Message\ResponseInterface $response PSR7 response
* @param callable $next Next middleware
*
* @return \Psr\Http\Message\ResponseInterface
*/
public function __invoke(Request $request, Response $response, $next)
{
$mediaType = $request->getMediaType();
$method = strtolower($request->getMethod());
if (in_array($method, ['post', 'put', 'patch']) && '' !== (string) $request->getBody()) {
if (empty($mediaType) || $mediaType !== 'application/json') {
return $response->withStatus(415);
}
}
$response = $response->withHeader('Content-type', 'application/json');
$response = $next($request, $response);
return $response;
}