Zttp is a Wrapper Around Guzzle for Simplifying Common Use Cases

Zttp is a Wrapper Around Guzzle for Simplifying Common Use Cases

Zttp is a new PHP package by Adam Wathan that is a Guzzle wrapper designed to bring an expressive syntax and simplify common use cases. Here is an example of a Post request with a custom header:

$response = Zttp::withHeaders(['Fancy' => 'Pants'])->post($url, [
    'foo' => 'bar',
    'baz' => 'qux',
]);

$response->json();

A comparable one with Guzzle would look something like this:

$client = new Client();
$response = $client->request('POST', $url, [
    'headers' => [
        'Fancy' => 'Pants',
    ],
    'form_params' => [
        'foo' => 'bar',
        'baz' => 'qux',
    ]
]);

json_decode($response->getBody());

As you can see, Zttp simplifies the code to make the request and automatically returns the JSON response.

Here is a few more example from Zttp:

Perform a Post with Form Params

$response = Zttp::asFormParams()->post($url, [
    'foo' => 'bar',
    'baz' => 'qux',
]);

Patch Request

$response = Zttp::patch($this->url('/patch'), [
    'foo' => 'bar',
    'baz' => 'qux',
]);

Put Request

$response = Zttp::put($this->url('/put'), [
    'foo' => 'bar',
    'baz' => 'qux',
]);

Delete Request

$response = Zttp::delete($this->url('/delete'), [
    'foo' => 'bar',
    'baz' => 'qux',
]);

Add an Accept Header

$response = Zttp::accept('banana/sandwich')->post($url);

Prevent Redirects

$response = Zttp::withoutRedirecting()->get($url);

There are several other examples in the Zttp test file. The package is still under development, and you can listen to the latest Full Stack Radio for the back stories. For more information check out the GitHub project.


Filed in: News / Guzzle


Newsletter

Join the weekly newsletter and never miss out on new tips, tutorials, and more.

Laravel News Partners

Laravel Jobs

Senior Laravel Developer (Full-Stack)
Munich
Volunteer Vision GmbH
Full-Stack Developer
Tampa
Nu Image Medical
Senior Software Developer
Remote (US ONLY)/Kenilworth, NJ
Diversified
Senior Back-end Laravel API Developer - Immediate Contract (Full Time)
Remote
ApproveMe // Document Signing
Web Development Project Manager
Houston, TX
Swyft Filings
Experienced Laravel/VueJS Developer (Freelance)
Remote (preferably in the US)
Prosperly LLC
Senior React / Laravel Developer
Remote
The C.A.S.E. Engineering Group