PHP code example of moccalotto / hayttp

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

    

moccalotto / hayttp example snippets


use Hayttp\Hayttp;

$response = Hayttp::get($url)->send();

$response = Hayttp::get($url)->send();

$response = Hayttp::post($url)
    ->acceptJson()
    ->sendJson([
        'this' => 'associative',
        'array' => 'will',
        'be' => 'converted',
        'to' => 'a',
        'json' => 'object',
    ]);

$response = Hayttp::delete($url)
    ->acceptXml()
    ->send();

$stdClass = Hayttp::get($url)
    ->acceptJson()
    ->send()
    ->jsonDecoded();

$simpleXmlElement = Hayttp::get($url)
    ->acceptXml()
    ->send()
    ->xmlDecoded();

$array = Hayttp::get($url)
    ->send()
    ->urlDecoded();

$variable = Hayttp::get($url)->send()->decoded();

$body = hayttp()->withTimeout(10)
    ->post($url)
    ->ensureJson()
    ->sendJson(['foo' => 'bar',])
    ->decded();

// All the calls below are equivalent

$response = hayttp_get($url);

$response = Hayttp::get($url)
                ->ensure2xx()
                ->send();

$response = hayttp()->get($url)
                ->ensure2xx()
                ->send();

// All the calls below are equivalent
$xml = new SimpleXmlElement('<root><groot>Boot</groot></root>');

$response = hayttp_post($url, $xml);

$response = Hayttp::post($url)
                ->ensure2xx()
                ->sendXml($xml);

$response = hayttp()->post($url)
                ->ensure2xx()
                ->sendXml($xml);

// All the calls below are equivalent
$data = ['foo' => ['bar' => ['baz']]];

$response = hayttp_post($url, $data);

$response = Hayttp::post($url)
                ->ensure2xx()
                ->sendJson($data);

$response = hayttp()->post($url)
                ->ensure2xx()
                ->sendJson($data);

// All the calls below are equivalent
$raw = file_get_contents($path);

$response = hayttp_put($url, $raw);

$response = Hayttp::put($url)
                ->ensure2xx()
                ->sendRaw($raw, 'application/octet-stream');

$response = hayttp()->put($url)
                ->ensure2xx()
                ->sendRaw($raw, 'application/octet-stream');