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
PHP's lightweight HTTP client
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
Httpful is a user-friendly PHP HTTP client library designed to make sending HTTP requests simple and intuitive. It provides a fluent interface for building and sending requests, handling responses, and managing various HTTP-related tasks with minimal code.
Pros
- Fluent and chainable API for easy request construction
- Built-in support for various content types (JSON, XML, form-encoded)
- Automatic parsing of response bodies based on content type
- Supports both synchronous and asynchronous requests
Cons
- Not actively maintained (last commit was in 2017)
- Limited support for modern PHP features and practices
- Lacks some advanced features found in more recent HTTP clients
- May have compatibility issues with newer PHP versions
Code Examples
- Basic GET request:
$response = \Httpful\Request::get('https://api.example.com/users')
->send();
echo $response->body;
- POST request with JSON payload:
$response = \Httpful\Request::post('https://api.example.com/users')
->sendsJson()
->body(['name' => 'John Doe', 'email' => 'john@example.com'])
->send();
echo $response->body->id;
- Handling authentication:
$response = \Httpful\Request::get('https://api.example.com/protected')
->authenticateWith('username', 'password')
->send();
if ($response->code == 200) {
echo "Authentication successful!";
}
Getting Started
- Install Httpful using Composer:
composer require nategood/httpful
- Include the Composer autoloader in your PHP script:
require 'vendor/autoload.php';
- Start making HTTP requests:
use \Httpful\Request;
$response = Request::get('https://api.example.com/data')
->expectsJson()
->send();
if ($response->code == 200) {
$data = $response->body;
// Process the data
} else {
echo "Error: " . $response->code;
}
Competitor Comparisons
Guzzle, an extensible PHP HTTP client
Pros of Guzzle
- More feature-rich and flexible, supporting advanced HTTP features like concurrent requests and streaming
- Better maintained with more frequent updates and a larger community
- Follows PSR-7 standards for HTTP message interfaces
Cons of Guzzle
- Steeper learning curve due to its extensive feature set
- Larger footprint and potentially more dependencies
Code Comparison
Httpful:
$response = \Httpful\Request::get('http://example.com/api')
->expectsJson()
->send();
echo $response->body->message;
Guzzle:
$client = new \GuzzleHttp\Client();
$response = $client->request('GET', 'http://example.com/api');
echo json_decode($response->getBody())->message;
Summary
Guzzle is a more comprehensive HTTP client library with a wider range of features and better community support. It's ideal for complex projects requiring advanced HTTP functionality. Httpful, on the other hand, offers a simpler, more intuitive API that may be preferable for smaller projects or developers new to HTTP clients in PHP. While Guzzle adheres to modern PHP standards like PSR-7, it comes with a steeper learning curve and a larger footprint. The choice between the two depends on the specific needs of your project and your familiarity with HTTP concepts.
Provides powerful methods to fetch HTTP resources synchronously or asynchronously
Pros of Http-client
- Part of the larger Symfony ecosystem, offering better integration with other Symfony components
- More actively maintained with frequent updates and bug fixes
- Supports both synchronous and asynchronous requests
Cons of Http-client
- Steeper learning curve due to its more complex architecture
- Requires more setup and configuration compared to Httpful's simpler approach
- Larger footprint and potentially higher resource usage
Code Comparison
Http-client:
$client = HttpClient::create();
$response = $client->request('GET', 'https://api.example.com');
$content = $response->getContent();
Httpful:
$response = \Httpful\Request::get('https://api.example.com')->send();
$content = $response->body;
Summary
Http-client is a more robust and feature-rich solution, ideal for larger projects and those already using Symfony components. It offers better performance and more advanced features but comes with a steeper learning curve.
Httpful, on the other hand, provides a simpler and more intuitive API, making it easier to use for smaller projects or quick prototypes. However, it lacks some of the advanced features and performance optimizations found in Http-client.
The choice between the two depends on the project's requirements, existing technology stack, and the developer's familiarity with Symfony components.
HTTPlug, the HTTP client abstraction for PHP
Pros of httplug
- Offers a more flexible and modular approach with its plugin system
- Provides better interoperability between different HTTP clients
- Supports asynchronous requests out of the box
Cons of httplug
- Steeper learning curve due to its more complex architecture
- Requires additional setup and configuration compared to Httpful
Code Comparison
httplug:
$client = HttpClientDiscovery::find();
$request = new Request('GET', 'http://example.com');
$response = $client->sendRequest($request);
Httpful:
$response = \Httpful\Request::get('http://example.com')->send();
Summary
httplug is a more powerful and flexible HTTP client library, offering a plugin system and better interoperability. It supports asynchronous requests and allows for easy switching between different HTTP clients. However, it comes with a steeper learning curve and requires more setup.
Httpful, on the other hand, provides a simpler and more straightforward API, making it easier to use for basic HTTP requests. It has a fluent interface that allows for quick and readable code. However, it lacks some of the advanced features and flexibility offered by httplug.
The choice between the two libraries depends on the specific needs of your project. For simple HTTP requests, Httpful might be sufficient, while httplug is better suited for more complex scenarios or when interoperability is a key concern.
PHP's lightweight HTTP client
Pros of Buzz
- More actively maintained with recent updates
- Supports asynchronous requests and parallel processing
- Offers middleware support for request/response manipulation
Cons of Buzz
- Steeper learning curve due to more advanced features
- Larger codebase and potentially higher overhead
- Less focus on simplicity and ease of use for basic HTTP requests
Code Comparison
Httpful:
$response = \Httpful\Request::get('http://example.com/api')
->expectsJson()
->send();
echo $response->body->message;
Buzz:
$browser = new Buzz\Browser();
$response = $browser->get('http://example.com/api');
$data = json_decode($response->getContent(), true);
echo $data['message'];
Both libraries provide straightforward ways to make HTTP requests, but Buzz offers more flexibility and advanced features at the cost of slightly more verbose code. Httpful focuses on a more fluent, chainable API for simpler use cases, while Buzz provides a more robust foundation for complex HTTP interactions and parallel processing.
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 recent updates
- Wider adoption and community support
- Better documentation and examples
Cons of Requests
- Slightly more complex API for basic usage
- Requires more setup for advanced features
Code Comparison
Httpful:
$response = \Httpful\Request::get($url)->send();
echo $response->body;
Requests:
$response = Requests::get($url);
echo $response->body;
Both libraries offer similar functionality for basic HTTP requests, with Httpful providing a more fluent interface and Requests offering a more straightforward approach. Httpful uses method chaining, while Requests uses separate method calls for additional options.
Requests is generally considered more robust and feature-rich, with better support for complex scenarios like authentication and file uploads. It also has better integration with WordPress core, making it a preferred choice for WordPress-related projects.
Httpful, on the other hand, offers a simpler API for quick and easy HTTP requests, which may be preferable for smaller projects or those requiring minimal HTTP functionality.
Ultimately, the choice between the two libraries depends on the specific project requirements, integration needs, and personal preference for API style.
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
Httpful
Httpful is a simple Http Client library for PHP 8.0+. There is an emphasis of readability, simplicity, and flexibility â basically provide the features and flexibility to get the job done and make those features really easy to use.
Features
- Readable HTTP Method Support (GET, PUT, POST, DELETE, HEAD, PATCH and OPTIONS)
- Custom Headers
- Automatic "Smart" Parsing
- Automatic Payload Serialization
- Basic Auth
- Client Side Certificate Auth
- Request "Templates"
Sneak Peak
Here's something to whet your appetite. Search the twitter API for tweets containing "#PHP". Include a trivial header for the heck of it. Notice that the library automatically interprets the response as JSON (can override this if desired) and parses it as an array of objects.
// Make a request to the GitHub API with a custom
// header of "X-Trvial-Header: Just as a demo".
$url = "https://api.github.com/users/nategood";
$response = \Httpful\Request::get($url)
->expectsJson()
->withXTrivialHeader('Just as a demo')
->send();
echo "{$response->body->name} joined GitHub on " .
date('M jS', strtotime($response->body->created_at)) ."\n";
Installation
Composer
Httpful is PSR-0 compliant and can be installed using composer. Simply add nategood/httpful
to your composer.json file. Composer is the sane alternative to PEAR. It is excellent for managing dependencies in larger projects.
{
"require": {
"nategood/httpful": "*"
}
}
Install from Source
Because Httpful is PSR-0 compliant, you can also just clone the Httpful repository and use a PSR-0 compatible autoloader to load the library, like Symfony's. Alternatively you can use the PSR-0 compliant autoloader included with the Httpful (simply require("bootstrap.php")
).
Build your Phar
If you want the build your own Phar Archive you can use the build
script included.
Make sure that your php.ini
has the Off or 0 value for the phar.readonly
setting.
Also you need to create an empty downloads
directory in the project root.
Contributing
Httpful highly encourages sending in pull requests. When submitting a pull request please:
- All pull requests should target the
dev
branch (notmaster
) - Make sure your code follows the coding conventions
- Please use soft tabs (four spaces) instead of hard tabs
- Make sure you add appropriate test coverage for your changes
- Run all unit tests in the test directory via
phpunit ./tests
- Include commenting where appropriate and add a descriptive pull request message
Changelog
1.0.0
- SECURITY Make certificate validation the default. This is a potentially breaking change and as a result, bumping major version number in line with semver. Validation can still be skipped but must be explicitly skipped via withoutStrictSSL.
- REFACTOR PR #305 Remove deprecated functionality pre PHP 8.1
- REFACTOR Partially from PR #282 Add CI support for running tests now that Travis is gone.
0.3.2
- REFACTOR PR #276 Add properly subclassed, more descriptive Exceptions for JSON parse errors
0.3.1
- FIX PR #286 Fixed header case sensitivity
0.3.0
- REFACTOR Dropped support for dead versions of PHP. Updated the PHPUnit tests.
0.2.20
- MINOR Move Response building logic into separate function PR #193
0.2.19
0.2.18
0.2.17
- FEATURE PR #144 Adds additional parameter to the Response class to specify additional meta data about the request/response (e.g. number of redirect).
0.2.16
- FEATURE Added support for whenError to define a custom callback to be fired upon error. Useful for logging or overriding the default error_log behavior.
0.2.15
- FEATURE I #131 Support for SOCKS proxy
0.2.14
- FEATURE I #138 Added alternative option for XML request construction. In the next major release this will likely supplant the older version.
0.2.13
- REFACTOR I #121 Throw more descriptive exception on curl errors
- REFACTOR I #122 Better proxy scrubbing in Request
- REFACTOR I #119 Better document the mimeType param on Request::body
- Misc code and test cleanup
0.2.12
- REFACTOR I #123 Support new curl file upload method
- FEATURE I #118 5.4 HTTP Test Server
- FIX I #109 Typo
- FIX I #103 Handle also CURLOPT_SSL_VERIFYHOST for strictSsl mode
0.2.11
- FIX I #99 Prevent hanging on HEAD requests
0.2.10
- FIX I #93 Fixes edge case where content-length would be set incorrectly
0.2.9
- FEATURE I #89 multipart/form-data support (a.k.a. file uploads)! Thanks @dtelaroli!
0.2.8
- FIX Notice fix for Pull Request 86
0.2.7
- FIX I #86 Remove Connection Established header when using a proxy
0.2.6
- FIX I #85 Empty Content Length issue resolved
0.2.5
0.2.4
- FEATURE I #77 Convenience method for setting a timeout (seconds)
$req->timeoutIn(10);
- FIX I #75 I #78 Bug with checking if digest auth is being used.
0.2.3
- FIX Overriding default Mime Handlers
- FIX PR #73 Parsing http status codes
0.2.2
- FEATURE Add support for parsing JSON responses as associative arrays instead of objects
- FEATURE Better support for setting constructor arguments on Mime Handlers
0.2.1
- FEATURE PR #72 Allow support for custom Accept header
0.2.0
- REFACTOR PR #49 Broke headers out into their own class
- REFACTOR PR #54 Added more specific Exceptions
- FIX PR #58 Fixes throwing an error on an empty xml response
- FEATURE PR #57 Adds support for digest authentication
0.1.6
- Ability to set the number of max redirects via overloading
followRedirects(int max_redirects)
- Standards Compliant fix to
Accepts
header - Bug fix for bootstrap process when installed via Composer
0.1.5
- Use
DIRECTORY_SEPARATOR
constant PR #33 - PR #35
- Added the raw_headers property reference to response.
- Compose request header and added raw_header to Request object.
- Fixed response has errors and added more comments for clarity.
- Fixed header parsing to allow the minimum (status line only) and also cater for the actual CRLF ended headers as per RFC2616.
- Added the perfect test Accept: header for all Acceptable scenarios see @b78e9e82cd9614fbe137c01bde9439c4e16ca323 for details.
- Added default User-Agent header
User-Agent: Httpful/0.1.5
+ curl version + server software + PHP version- To bypass this "default" operation simply add a User-Agent to the request headers even a blank User-Agent is sufficient and more than simple enough to produce me thinks.
- Completed test units for additions.
- Added phpunit coverage reporting and helped phpunit auto locate the tests a bit easier.
0.1.4
- Add support for CSV Handling PR #32
0.1.3
- Handle empty responses in JsonParser and XmlParser
0.1.2
- Added support for setting XMLHandler configuration options
- Added examples for overriding XmlHandler and registering a custom parser
- Removed the httpful.php download (deprecated in favor of httpful.phar)
0.1.1
- Bug fix serialization default case and phpunit tests
0.1.0
- Added Support for Registering Mime Handlers
- Created AbstractMimeHandler type that all Mime Handlers must extend
- Pulled out the parsing/serializing logic from the Request/Response classes into their own MimeHandler classes
- Added ability to register new mime handlers for mime types
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
PHP's lightweight HTTP client
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