Skip to content
This repository has been archived by the owner on Oct 3, 2021. It is now read-only.

brokeyourbike/php-http-client

 
 

Repository files navigation

Travis Badge Latest Version on Packagist GitHub contributors MIT licensed

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:

Thanks

We were inspired by the work done on birdy and universalclient.

License

The MIT License (MIT)

About

BrokeYourBike's PHP HTTP Client for calling APIs

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages

  • PHP 99.0%
  • Dockerfile 1.0%