PHP code example of pff / accept-header-service-provider

1. Go to this page and download the library: Download pff/accept-header-service-provider 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/ */

    

pff / accept-header-service-provider example snippets


    

    use Pff\ServiceProvider\AcceptHeaderServiceProvider\AcceptHeaderServiceProvider;
    
    $app->register(new AcceptHeaderServiceProvider());

    $app->get('/test', function($accept_header) {
        if ($accept_header == 'application/ven.test.v1+json')
            $cont = json_encode(array('content' => 'hello'));
        else
            $cont = '<content>hello</content>';

        return new Response($cont, 200, array('Content-Type' => $accept_header));
    })->accept(array('application/ven.test.v1+json', 'application/ven.test.v1+xml'));
  

    $app->get('/test', function($accept_header) {
        if ($accept_header == 'application/ven.test.v2+json')
            $cont = json_encode(array('content' => 'hiya'));
        else
            $cont = '<content>hiya</content>';

        return new Response($cont, 200, array('Content-Type' => $accept_header));
    })->accept(array('application/ven.test.v2+json', 'application/ven.test.v2+xml'));