Httpful is a simple Http Client library for PHP 7.2+. 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 (not master)
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.
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.
请发表评论