PHP code example of caridea / http

1. Go to this page and download the library: Download caridea/http 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/ */

    

caridea / http example snippets


use Caridea\Http\ProblemDetails;
use Zend\Diactoros\Uri;

$problem = new ProblemDetails(
    new Uri('http://example.com/problem/oops'),  // type
    'A weird thing happened',                    // title
    500,                                         // status
    'It looks like the server has goofed again', // detail
    new Uri('http://example.com/problems/1f9a'), // instance
    [                                            // extensions
        'server' => 'workerbee01.example.com',
        'auth' => 'foobar'
    ]
);
echo json_encode($problem);


use Zend\Diactoros\ServerRequestFactory;

$request = ServerRequestFactory::fromGlobals(
    $_SERVER,
    \Caridea\Http\QueryParams::getFromServer(), // instead of $_GET
);

$factory = new \Caridea\Http\PaginationFactory();

// say the Query was ?count=25&startIndex=1&sort=%2Bfoo&sort-bar
// or maybe          ?count=25&start=0&sort=%2Bfoo,-bar
// or one of many other formats for this type of pagination settingns
$pagination = $factory->create($request, 'sort');
$pagination->getMax();    // 25
$pagination->getOffset(); // 0
$pagination->getOrder();  // ['foo' => true, 'bar' => false]

// say the HTTP_ACCEPT field is text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
$types = new \Caridea\Http\AcceptTypes($_SERVER);
$types->preferred(['application/xml', 'application/json']); // returns application/xml