Zttp is an guzzle package written by Adam Wathan to make the code more expressive and to simplify common use cases. In PHP projects, if you need to launch HTTP requests through code, I believe a lot of people are familiar with the package guzzlehttp, but in fact, when using guzzle, we can still do a little bit easier, This article we will share with you zttp simplified guzzle invocation instances, I hope to help everyone.
This is an example of using Zttp to post a custom header content request:
$response = Zttp::withheaders ([' Fancy ' = ' Pants '])->post ($url, [ ' foo ' = ' bar ', ' baz ' = ' qux ',]) ; $response->json ();
If you write this request with something similar to guzzle, you might write:
$client = new Client () $response = $client->request (' POST ', $url, [ ' headers ' = = [' Fancy ' = ' Pants ',
], ' form_params ' = [ ' foo ' = ' bar ', ' baz ' = ' qux ', ]]; Json_decode ($response GetBody ());
By contrast, Zttp simplifies the way code is written, and it simply returns JSON-formatted content.
Here are a few examples of using zttp:
Post Request with Parameters #
$response = Zttp::asformparams ()->post ($url, [ ' foo ' = ' bar ', ' baz ' = ' qux ',]);
Patch Request #
$response = zttp::p atch ($this->url ('/patch '), [ ' foo ' = ' bar ', ' baz ' = ' qux ',]);
Put Request #
$response = zttp::p ut ($this->url ('/put '), [ ' foo ' = ' bar ', ' baz ' = ' qux ',]);
Delete Request #
$response = zttp::d elete ($this->url ('/delete '), [ ' foo ' = ' bar ', ' baz ' = ' qux ',]);
Add Request Header #
$response = zttp::accept (' Banana/sandwich ')->post ($url);
Prevent redirection #
$response = Zttp::withoutRedirecting()->get($url);