PHP code example of evaisse / simple-http-bundle

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

    

evaisse / simple-http-bundle example snippets


$http = $this->container->get('http');
$http = $this->get('http');

// same as  

try {
    $http->GET('http://httpbin.org/ip'); // yeah, that's all.
} catch (\Symfony\Component\HttpKernel\Exception\HttpException $e) {
    // handle errors
}

try {
    $data = $http->POST('http://httpbin.org/post', $myArgs);
} catch (\Symfony\Component\HttpKernel\Exception\HttpException $e) {
    // handle errors
}

$data = $http->GET('http://httpbin.org/status/{code}', array(
    "code" => 200
));
// will call http://httpbin.org/status/200

$data = $http->GET('http://httpbin.org/status/{code}', array(
    "code" => 200,
    "foo" => "bar" 
));
// will call http://httpbin.org/status/200?foo=bar

$transac = $http->prepare('GET', 'http://httpbin.org/ip');

$transac->execute();

$a = $http->prepare('GET', 'http://httpbin.org/ip');
$b = $http->prepare('PUT', 'http://httpbin.org/put');
$c = $http->prepare('POST', 'http://httpbin.org/post');

$http->execute([
    $a, 
    $b,
    $c
]);

$a->hasError() || $a->getResult();
$b->hasError() || $b->getResult();
$c->hasError() || $c->getResult();

print $http->prepare('POST', 'http://httpbin.org/ip', $_SERVER)
           ->json()
           ->execute()
           ->getResult()['ip'];

$http->prepare('PUT', 'http://httpbin.org/put')
     ->addFile('f1', './myfile.txt')
     ->addFile('f2', './myfile.txt')
     ->execute();


$http->prepare('POST', 'http://httpbin.org/post', [
        'infos' => 'foo',
        'bar'   => 'so so',
    ])
     ->addFile('f1', './myfile.txt')
     ->addFile('f2', './myfile.txt')
     ->execute();

$a = $http->prepare('GET',  'http://httpbin.org/ip');
$b = $http->prepare('PUT',  'http://httpbin.org/put');
$c = $http->prepare('POST', 'http://httpbin.org/post');


$cookies = $http->getDefaultCookieJar();
 // $cookies = $http->getCookieJar($session); if you want to directly store in user session

$http->execute([
    $a, 
    $b,
    $c
], $cookies);

dump($cookies);

$stmt = $http->prepare('PUT', 'http://httpbin.org/put')

$stmt->onSuccess(function ($data) {
    // handle data
})->onError(function (\Symfony\Component\HttpKernel\Exception\HttpException $e) {
    // handle errors
})->onFinish(function () {
    // like "finally"
});

$http->execute([
    $stmt
]);