Updated 21 January 2021
Zttp is a simple Guzzle wrapper designed to provide a really pleasant development experience for most common use cases. It is a new PHP package by Adam Wathan that is a Guzzle wrapper designed to bring an expressive syntax and simplify common use cases.
It provides a much nicer syntax for the use case of Guzzle where you just need to POST some JSON to an endpoint.
let’s find out the basic usage of this new http package.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
use Illuminate\Support\Facades\Http; $response = Http::post('url', [ 'name' => 'Webkul', ]); echo $response['foo']; $response->body() $response->json() $response->status() $response->ok() $response->successful() (>= 200 && < 300) $response->serverError() $response->clientError() |
Well, yes it is an authenticated news coming directly from Taylor Otwell.
He merged the request on github and mentioned:
This is a port of Adam Wathan’s ZTTP Guzzle convenience layer. It provides much nicer syntax for the 90% use case of Guzzle where you just need to POST some JSON to an endpoint. In addition, this supplements ZTTP with stubbing / faking inspired by jason mccreary’s work on that library.
This is not an entirely new client – it is only a UX / DX convenience layer on top of Guzzle. We will not be adding a lot of complicated features to this. If you need very, very robust or complicated logic just use Guzzle directly. We aren’t trying to expose every feature of Guzzle through this API.
With the help of Zttp, headers can be added using the withHeaders method.
1 2 3 |
$response = Http::withHeaders(['X-Foo' => 'bar'])->post('url', [ 'name' => 'Webkul', ]); |
1 2 3 |
$response = Http::withToken('token')->post('url', [ 'name' => 'Webkul', ]); |
Along with that, you can now also use basic authentication.
1 2 3 |
$response = Http::withBasicAuth('username', 'password')->post('url', [ 'name' => 'Webkul', ]); |
By default, like ZTTP, exceptions are not thrown on client and server errors. If you receive a response that is not successful and you would like to throw an exception, you may call $response->throw().
1 2 3 4 5 |
$response = Http::post(...); if (! $response->successful()) { $response->throw(); } |
1 2 3 4 |
$response = Zttp::asFormParams()->post($url, [ 'foo' => 'bar', 'baz' => 'qux', ]); |
1 2 3 4 |
$response = Zttp::asFormParams()->post($url, [ 'foo' => 'bar', 'baz' => 'qux', ]); |
1 2 3 4 |
$response = Zttp::put($this->url('/put'), [ 'foo' => 'bar', 'baz' => 'qux', ]); |
1 2 3 4 |
$response = Zttp::delete($this->url('/delete'), [ 'foo' => 'bar', 'baz' => 'qux', ]); |
Sources:
Laravel News
PR on GitHub
If you have more details or questions, you can reply to the received confirmation email.
Back to Home
Be the first to comment.