getAcceptableContentTypes() public method

This is determined by the Accept HTTP header. For example, php $_SERVER['HTTP_ACCEPT'] = 'text/plain; q=0.5, application/json; version=1.0, application/xml; version=2.0;'; $types = $request->getAcceptableContentTypes(); print_r($types); displays: [ 'application/json' => ['q' => 1, 'version' => '1.0'], 'application/xml' => ['q' => 1, 'version' => '2.0'], 'text/plain' => ['q' => 0.5], ]
public getAcceptableContentTypes ( ) : array
return array the content types ordered by the quality score. Types with the highest scores will be returned first. The array keys are the content types, while the array values are the corresponding quality score and other parameters as given in the header.
Example #1
0
 /**
  * Negotiates the response format.
  * @param Request $request
  * @param Response $response
  * @throws InvalidConfigException if [[formats]] is empty
  * @throws UnsupportedMediaTypeHttpException if none of the requested content types is accepted.
  */
 protected function negotiateContentType($request, $response)
 {
     if (!empty($this->formatParam) && ($format = $request->get($this->formatParam)) !== null) {
         if (in_array($format, $this->formats)) {
             $response->format = $format;
             $response->acceptMimeType = null;
             $response->acceptParams = [];
             return;
         } else {
             throw new UnsupportedMediaTypeHttpException('The requested response format is not supported: ' . $format);
         }
     }
     $types = $request->getAcceptableContentTypes();
     if (empty($types)) {
         $types['*/*'] = [];
     }
     foreach ($types as $type => $params) {
         if (isset($this->formats[$type])) {
             $response->format = $this->formats[$type];
             $response->acceptMimeType = $type;
             $response->acceptParams = $params;
             return;
         }
     }
     if (isset($types['*/*'])) {
         // return the first format
         foreach ($this->formats as $type => $format) {
             $response->format = $this->formats[$type];
             $response->acceptMimeType = $type;
             $response->acceptParams = [];
             return;
         }
     }
     throw new UnsupportedMediaTypeHttpException('None of your requested content types is supported.');
 }