brokeyourbike / php-http-client
HTTP REST client, simplified for PHP
Requires
- php: >=5.6
Requires (Dev)
- phpunit/phpunit: ~4.4
- squizlabs/php_codesniffer: ~2.0
README
Quickly and easily access any RESTful or RESTful-like API.
Announcements
All updates to this library is documented in our CHANGELOG.
Table of Contents
Installation
Prerequisites
- PHP version 5.6 or higher
Install with Composer
Add php-http-client to your composer.json
file. If you are not using Composer, you should be. It's an excellent way to manage dependencies in your PHP application.
{ "require": { "brokeyourbike/php-http-client": "~3.10.0" } }
Then at the top of your PHP script require the autoloader:
require __DIR__ . '/vendor/autoload.php';
Then from the command line:
composer install
Quick Start
Here is a quick example:
GET /your/api/{param}/call
// include __DIR__ . '/loader.php'; require 'vendor/autoload.php'; $apiKey = YOUR_SENDGRID_API_KEY; $authHeaders = [ 'Authorization: Bearer ' . $apiKey ]; $client = new BrokeYourBike\Client('https://api.sendgrid.com', $authHeaders); $param = 'foo'; $response = $client->your()->api()->_($param)->call()->get(); var_dump( $response->statusCode(), $response->headers(), $response->body() );
POST /your/api/{param}/call
with headers, query parameters and a request body with versioning.
// include __DIR__ . '/loader.php'; require 'vendor/autoload.php'; $apiKey = YOUR_SENDGRID_API_KEY; $authHeaders = [ 'Authorization: Bearer ' . $apiKey ]; $client = new BrokeYourBike\Client('https://api.sendgrid.com', $authHeaders); $queryParams = [ 'hello' => 0, 'world' => 1 ]; $requestHeaders = [ 'X-Test' => 'test' ]; $data = [ 'some' => 1, 'awesome' => 2, 'data' => 3 ]; $param = 'bar'; $response = $client->your()->api()->_($param)->call()->post($data, $queryParams, $requestHeaders); var_dump( $response->statusCode(), $response->headers(), $response->body() );
If there is an issues with the request, such as misconfigured CURL SSL options, an InvalidRequest
will be thrown
with message from CURL on why the request failed. Use the message as a hit to troubleshooting steps of your environment.
Usage
Roadmap
If you are interested in the future direction of this project, please take a look at our milestones. We would love to hear your feedback.
How to Contribute
We encourage contribution to our libraries, please see our CONTRIBUTING guide for details.
Quick links:
- Feature Request
- Bug Reports
- Sign the CLA to Create a Pull Request
- Improvements to the Codebase
- Review Pull Requests
Thanks
We were inspired by the work done on birdy and universalclient.