PHP code example of ejetar / accept-header-interpreter

1. Go to this page and download the library: Download ejetar/accept-header-interpreter library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ejetar / accept-header-interpreter example snippets


use Ejetar\AcceptHeaderInterpreter\AcceptHeaderInterpreter;

try {
  $content = request()->headers->get('Accept'); //return accept header content
  //let's assume that $content is now: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
  
  $acceptHeaderInterpreter = new AcceptHeaderInterpreter($content);
  dd($acceptHeaderInterpreter->toCollection());
  
} catch (\Exception $ex) {
  echo $ex->getMessage();
}

/* the code above will print
Collection {#756 ▼
  #items: array:6 [▼
    0 => array:3 [▼
      "type" => "text"
      "subtype" => "html"
      "parameters" => array:1 [▼
        "q" => 1
      ]
    ]
    1 => array:3 [▼
      "type" => "application"
      "subtype" => "xhtml+xml"
      "parameters" => array:1 [▼
        "q" => 1
      ]
    ]
    3 => array:3 [▼
      "type" => "image"
      "subtype" => "webp"
      "parameters" => array:1 [▼
        "q" => 1
      ]
    ]
    4 => array:3 [▼
      "type" => "image"
      "subtype" => "apng"
      "parameters" => array:1 [▼
        "q" => 1
      ]
    ]
    2 => array:3 [▼
      "type" => "application"
      "subtype" => "xml"
      "parameters" => array:1 [▼
        "q" => "0.9"
      ]
    ]
    5 => array:3 [▼
      "type" => "*"
      "subtype" => "*"
      "parameters" => array:1 [▼
        "q" => "0.8"
      ]
    ]
  ]
}

REALIZE THAT THE ORDER OF MEDIA TYPES HAS BEEN MODIFIED
*/

use Ejetar\AcceptHeaderInterpreter\AcceptHeaderInterpreter;

try {
  $content = request()->headers->get('Accept'); //return accept header content
  //let's assume that $content is now: text/html,application/xhtml+xml,application/xml;q=1.1,image/webp,image/apng,*/*;q=0.8
  //It is only allowed to inform values from 0 to 1 for the parameter Q, that is, the contents of this Accept Header is invalid
  
  $acceptHeaderInterpreter = new AcceptHeaderInterpreter($content);
  dd($acceptHeaderInterpreter->toCollection());
  
} catch (\Exception $ex) {
  echo $ex->getMessage();
}

/* the code above will print
Accept header value is invalid!
*/

use Ejetar\AcceptHeaderInterpreter\AcceptHeaderInterpreter;

try {
  $content = request()->headers->get('Accept'); //return accept header content
  //let's assume that $content is now: */*, application/*, text/html, application/xhtml+xml
  
  $acceptHeaderInterpreter = new AcceptHeaderInterpreter($content);
  dd($acceptHeaderInterpreter->toCollection());
  
} catch (\Exception $ex) {
  echo $ex->getMessage();
}

/* the code above will print
Collection {#756 ▼
  #items: array:4 [▼
    2 => array:3 [▼
      "type" => " text"
      "subtype" => "html"
      "parameters" => array:1 [▼
        "q" => 1
      ]
    ]
    3 => array:3 [▼
      "type" => " application"
      "subtype" => "xhtml+xml"
      "parameters" => array:1 [▼
        "q" => 1
      ]
    ]
    1 => array:3 [▼
      "type" => " application"
      "subtype" => "*"
      "parameters" => array:1 [▼
        "q" => 1
      ]
    ]
    0 => array:3 [▼
      "type" => "*"
      "subtype" => "*"
      "parameters" => array:1 [▼
        "q" => 1
      ]
    ]
  ]
}

NOTE THAT ORDER OF THE MEDIA TYPES IS NO LONGER THE SAME, IT WAS MODIFIED AS A PRIORITY, ACCORDING TO THE SPECIFICATION.
*/

use Ejetar\AcceptHeaderInterpreter\AcceptHeaderInterpreter;

try {
  $content = request()->headers->get('Accept'); //return accept header content
  //let's assume that $content is now: application/json
  
  $acceptHeaderInterpreter = new AcceptHeaderInterpreter($content);
  echo $acceptHeaderInterpreter->getOriginalContent();
  
} catch (\Exception $ex) {
  echo $ex->getMessage();
}

/* the code above will print
application/json
*/