Top Related Projects
Guzzle, an extensible PHP HTTP client
Provides powerful methods to fetch HTTP resources synchronously or asynchronously
HTTPlug, the HTTP client abstraction for PHP
A Chainable, REST Friendly, PHP HTTP Client. A sane alternative to cURL.
Requests for PHP is a humble HTTP request library. It simplifies how you interact with other sites and takes away all your worries.
Quick Overview
Buzz is a lightweight PHP HTTP client library designed for simplicity and ease of use. It provides a fluent interface for making HTTP requests and handling responses, supporting various HTTP methods, headers, and authentication mechanisms.
Pros
- Simple and intuitive API for making HTTP requests
- Supports both synchronous and asynchronous requests
- Extensible through middleware and plugins
- PSR-7 and PSR-18 compliant
Cons
- Limited built-in features compared to more comprehensive HTTP clients
- Requires additional dependencies for certain functionalities (e.g., cURL)
- Less active development compared to some alternatives
- May not be suitable for complex HTTP scenarios out of the box
Code Examples
- Making a simple GET request:
use Buzz\Browser;
use Buzz\Client\FileGetContents;
use Nyholm\Psr7\Factory\Psr17Factory;
$client = new FileGetContents(new Psr17Factory());
$browser = new Browser($client, new Psr17Factory());
$response = $browser->get('https://api.example.com/users');
echo $response->getStatusCode();
echo $response->getBody()->__toString();
- Sending a POST request with JSON data:
$response = $browser->post('https://api.example.com/users', [
'Content-Type' => 'application/json'
], json_encode([
'name' => 'John Doe',
'email' => 'john@example.com'
]));
- Using middleware for request/response manipulation:
use Buzz\Middleware\BasicAuthMiddleware;
$browser->addMiddleware(new BasicAuthMiddleware('username', 'password'));
$response = $browser->get('https://api.example.com/protected-resource');
Getting Started
To start using Buzz in your PHP project, follow these steps:
-
Install Buzz using Composer:
composer require kriswallsmith/buzz
-
Create a browser instance:
use Buzz\Browser; use Buzz\Client\FileGetContents; use Nyholm\Psr7\Factory\Psr17Factory; $client = new FileGetContents(new Psr17Factory()); $browser = new Browser($client, new Psr17Factory());
-
Make HTTP requests:
$response = $browser->get('https://api.example.com'); echo $response->getBody()->__toString();
Competitor Comparisons
Guzzle, an extensible PHP HTTP client
Pros of Guzzle
- More extensive feature set, including advanced request/response handling and middleware support
- Better documentation and larger community, leading to more resources and support
- Actively maintained with frequent updates and improvements
Cons of Guzzle
- Steeper learning curve due to its more complex architecture
- Larger footprint and potentially higher overhead for simple HTTP requests
- May be overkill for projects with basic HTTP client needs
Code Comparison
Buzz:
$browser = new Buzz\Browser();
$response = $browser->get('https://api.example.com');
$content = $response->getContent();
Guzzle:
$client = new GuzzleHttp\Client();
$response = $client->get('https://api.example.com');
$content = $response->getBody()->getContents();
Both libraries offer similar basic functionality for making HTTP requests. However, Guzzle provides more advanced features and configuration options, which can be beneficial for complex projects but may introduce unnecessary complexity for simpler use cases.
Buzz is generally more lightweight and straightforward, making it easier to use for basic HTTP operations. On the other hand, Guzzle's extensive feature set and active development make it a more versatile choice for larger projects or those requiring advanced HTTP client capabilities.
Provides powerful methods to fetch HTTP resources synchronously or asynchronously
Pros of Http-client
- Part of the Symfony ecosystem, offering seamless integration with other Symfony components
- More actively maintained with frequent updates and improvements
- Supports both synchronous and asynchronous requests out of the box
Cons of Http-client
- Steeper learning curve for developers not familiar with Symfony
- Heavier dependency footprint due to Symfony components
Code Comparison
Http-client:
use Symfony\Component\HttpClient\HttpClient;
$client = HttpClient::create();
$response = $client->request('GET', 'https://api.example.com');
$content = $response->getContent();
Buzz:
use Buzz\Browser;
use Buzz\Client\FileGetContents;
$client = new FileGetContents();
$browser = new Browser($client);
$response = $browser->get('https://api.example.com');
$content = $response->getBody()->__toString();
Both libraries provide similar functionality for making HTTP requests, but Http-client offers more advanced features and integration with the Symfony ecosystem. Buzz, on the other hand, is lighter and may be easier to use for simple projects or developers not familiar with Symfony. The choice between the two depends on the specific project requirements and the developer's familiarity with the respective ecosystems.
HTTPlug, the HTTP client abstraction for PHP
Pros of HTTPlug
- Provides a more flexible and decoupled HTTP client abstraction
- Supports PSR-7 HTTP message interfaces
- Offers better interoperability with various HTTP client implementations
Cons of HTTPlug
- Requires additional setup and configuration compared to Buzz
- May have a steeper learning curve for beginners
- Less opinionated, which can lead to more decision-making for developers
Code Comparison
HTTPlug:
$client = HttpClientDiscovery::find();
$request = RequestFactory::createRequest('GET', 'http://example.com');
$response = $client->sendRequest($request);
Buzz:
$client = new Browser();
$response = $client->get('http://example.com');
Additional Notes
HTTPlug focuses on providing a standardized interface for HTTP clients, allowing developers to switch between different implementations easily. It's particularly useful in library development where you want to avoid locking into a specific HTTP client.
Buzz, on the other hand, is a more straightforward and opinionated HTTP client library. It's easier to get started with and may be preferable for simpler projects or when you don't need the flexibility offered by HTTPlug.
Both libraries have their merits, and the choice between them depends on the specific requirements of your project and your preference for flexibility versus simplicity.
A Chainable, REST Friendly, PHP HTTP Client. A sane alternative to cURL.
Pros of Httpful
- More intuitive and fluent API for making HTTP requests
- Built-in support for various authentication methods (Basic, Digest, NTLM)
- Better handling of file uploads and multipart requests
Cons of Httpful
- Less actively maintained compared to Buzz
- Fewer options for configuring client behavior
- Limited support for asynchronous requests
Code Comparison
Httpful:
$response = \Httpful\Request::get($url)
->expectsJson()
->send();
echo $response->body->name;
Buzz:
$browser = new \Buzz\Browser();
$response = $browser->get($url);
$data = json_decode($response->getContent());
echo $data->name;
Both libraries provide simple ways to make HTTP requests, but Httpful offers a more fluent interface. Buzz requires more setup but provides greater flexibility in configuring the client.
Httpful's method chaining approach makes it easier to read and write, while Buzz's separation of browser and response objects allows for more granular control over the request and response handling process.
Ultimately, the choice between these libraries depends on the specific needs of your project, such as the complexity of requests, required features, and preference for API style.
Requests for PHP is a humble HTTP request library. It simplifies how you interact with other sites and takes away all your worries.
Pros of Requests
- More actively maintained with frequent updates
- Better documentation and examples
- Wider adoption in the WordPress ecosystem
Cons of Requests
- Limited to HTTP requests only
- Less flexible for advanced use cases
- Fewer built-in authentication methods
Code Comparison
Requests:
$response = Requests::get('https://api.example.com/data');
$data = json_decode($response->body);
Buzz:
$browser = new Browser();
$response = $browser->get('https://api.example.com/data');
$data = json_decode($response->getContent());
Both libraries offer simple ways to make HTTP requests, but Buzz provides more flexibility with its browser abstraction. Requests is more straightforward for basic use cases, while Buzz offers more advanced features for complex scenarios.
Requests is tailored for WordPress development, making it a better choice for WordPress-specific projects. Buzz, on the other hand, is a more general-purpose HTTP client library that can be used in various PHP applications.
In terms of performance, both libraries are relatively efficient. However, Buzz may have a slight edge in handling concurrent requests due to its support for asynchronous operations.
Overall, the choice between these libraries depends on the specific project requirements and the developer's familiarity with each library's ecosystem.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Buzz - Scripted HTTP browser
Buzz is a lightweight (<1000 lines of code) PHP 7.1 library for issuing HTTP requests. The library includes three
clients: FileGetContents
, Curl
and MultiCurl
. The MultiCurl
supports batch requests and HTTP2 server push.
Installation
Install by running:
composer require kriswallsmith/buzz
You do also need to install a PSR-17 request/response factory. Buzz uses that factory to create PSR-7 requests and responses. Install one from this list.
Example:
composer require nyholm/psr7
Usage
This page will just show you the basics, please read the full documentation.
use Buzz\Browser;
use Buzz\Client\FileGetContents;
$client = new FileGetContents(new Psr17ResponseFactory());
$browser = new Browser($client, new Psr17RequestFactory());
$response = $browser->get('https://www.google.com');
echo $browser->getLastRequest()."\n";
// $response is a PSR-7 object.
echo $response->getStatusCode();
You can also use the low-level HTTP classes directly.
use Buzz\Client\FileGetContents;
$request = new PSR7Request('GET', 'https://google.com/foo');
$client = new FileGetContents(new Psr17ResponseFactory());
$response = $client->sendRequest($request, ['timeout' => 4]);
echo $response->getStatusCode();
Note
The two new Psr17ResponseFactory()
and new Psr17RequestFactory()
are placeholders
for whatever PSR-17 factory you choose. If you use nyholm/psr7
then the example above
would start like:
use Buzz\Browser;
use Buzz\Client\FileGetContents;
use Nyholm\Psr7\Factory\Psr17Factory;
$client = new FileGetContents(new Psr17Factory());
$browser = new Browser($client, new Psr17Factory());
$response = $browser->get('https://www.google.com');
HTTP2 server push
Buzz MultiCurl client support HTTP2 server push.
use Buzz\Client\MultiCurl;
use Nyholm\Psr7\Factory\Psr17Factory;
use Nyholm\Psr7\Request;
$client = new MultiCurl(new Psr17Factory());
$start = microtime(true);
$response = $client->sendRequest(new Request('GET', 'https://http2.golang.org/serverpush', [], null, '2.0'));
$timeFirstRequest = microtime(true) - $start;
// Parse response to find asset version.
$body = $response->getBody()->__toString();
$id = null;
if (preg_match('#/serverpush/static/style.css\?([0-9]+)#sim', $body, $matches)) {
$id = $matches[1];
}
// Make two new requests
$start = microtime(true);
$client->sendRequest(new Request('GET', 'https://http2.golang.org/serverpush/static/style.css?'.$id));
$client->sendRequest(new Request('GET', 'https://http2.golang.org/serverpush/static/playground.js?'.$id));
$timeOtherRequests = microtime(true) - $start;
echo 'First: '.$timeFirstRequest."\n";
echo 'Other: '.$timeOtherRequests."\n";
Since the two other requests was pushed, we spend no time fetching those.
First: 1.04281
Other: 0.00027
You can configure what request you want to accept as pushed with the push_function_callback
option.
The Idea of Buzz
Buzz was created by Kris Wallsmith back in 2010. The project grown very popular over the years with more than 7 million downloads.
Since August 2017 Tobias Nyholm is maintaining this library. The idea of Buzz will still be the same, we should have a simple API and mimic browser behavior for easy testing. We should not reinvent the wheel and we should not be as powerful and flexible as other clients (ie Guzzle). We do, however, take performance very seriously.
We do love PSRs and this is a wish list of what PSR we would like to support:
- PSR-1 (Code style)
- PSR-2 (Code style)
- PSR-4 (Auto loading)
- PSR-7 (HTTP messages)
- PSR-17 (HTTP factories)
- PSR-18 (HTTP client)
The goal
Since the release of 1.0 Buzz has reached its goal of being a lightweight client that covers 90% of all use cases. There
are no plans to actively develop new features or change the existing API. There are alternatives for people that wants
an more actively maintained HTTP clients. One that is particularly popular and got a big community behind it is the
Symfony HTTP Client.
Contribute
Buzz is great because it is small, simple and yet flexible. We are always happy to receive bug reports and bug fixes. We are also looking forward to review a pull request with a new middleware, especially if the middleware covers a common use case.
We will probably not accept any configuration option or feature to any of the clients or the Browser.
Backwards Compatibility Promise
We take backwards compatibility very seriously as you should do with any open source project. We strictly follow Semver. Please note that Semver works a bit different prior version 1.0.0. Minor versions prior 1.0.0 are allow to break backwards compatibility.
Being greatly inspired by Symfony's bc promise, we have adopted their method of deprecating classes, interfaces and functions.
Running the tests
There are 2 kinds of tests for this library; unit tests and integration tests. They can be run separably by:
./vendor/bin/phpunit --testsuite Unit
./vendor/bin/phpunit --testsuite Integration
The integration tests makes real HTTP requests to a webserver. There are two different
webservers used by our integration tests. A real Nginx server and PHP's built in webserver.
The tests that runs with PHP's webserver are provided by php-http/client-integration-tests
.
To start the server, open terminal A and run:
./vendor/bin/http_test_server
The other type of integration tests are using Nginx. We use Docker to start the Nginx server.
docker build -t buzz/tests .
docker run -d -p 127.0.0.1:8022:80 buzz/tests
You are now ready to run the integration tests
./vendor/bin/phpunit --testsuite Integration
Test Server Push
To use HTTP/2 server push you need to run the very latest PHP version. PHP also need to use cUrl > 7.61.1 and be compiled with libnghttp2. You can use docker:
composer update
docker run -it --rm --name php-latest -v "$PWD":/usr/src/myapp -w /usr/src/myapp tommymuehle/docker-alpine-php-nightly \
php vendor/bin/phpunit tests/Integration/MultiCurlServerPushTest.php
Top Related Projects
Guzzle, an extensible PHP HTTP client
Provides powerful methods to fetch HTTP resources synchronously or asynchronously
HTTPlug, the HTTP client abstraction for PHP
A Chainable, REST Friendly, PHP HTTP Client. A sane alternative to cURL.
Requests for PHP is a humble HTTP request library. It simplifies how you interact with other sites and takes away all your worries.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot