Zttp Simplified guzzle Invocation instance sharing

Source: Internet
Author: User
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);

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.