/php-watson-api-bridge

:bridge_at_night: A PHP and Laravel wrapper for IBM Watson API

Primary LanguagePHPMIT LicenseMIT

PHP IBM Watson API Bridge

Latest Stable Version Latest Unstable Version Build Status StyleCI License Total Downloads

Introduction

PHP IBM Watson API Bridge, provides a simple and easy to use wrapper around the IBM Watson API. The library makes it easier for us to develop PHP apps that use the IBM Watson API.

License

PHP IBM Watson API Bridge is open-sourced software licensed under the MIT license

Installation

$ composer require findbrok/php-watson-api-bridge

Usage

Before using the package checkout Watson API Explorer, to get a sense of what you can and cannot do with Watson

require 'vendor/autoload.php'

use FindBrok\WatsonBridge\Bridge;

// Create a new bridge Object.
$bridge = new Bridge('username', 'password', 'baseUrl');

// Simple get request.
$queryParams = ['foo' => 'bar'];
$response = $bridge->get('uri', $queryParams);

// Simple post request.
$dataToPost = ['foo' => 'bar'];
$response = $bridge->post('uri', $dataToPost, 'json');

The Package uses Guzzle to perform requests, all your responses will be instances of GuzzleHttp\Psr7\Response


Integration with Laravel 5

As of version 1.1.x, PHP Watson API bridge adds a new Service Provider which integrates easily with Laravel 5.

If you are using Laravel >= 5.5, you can skip service registration and aliases registration thanks to Laravel auto package discovery feature.

First add the ServiceProvider to your app.php file:

'providers' => [
   ....
   FindBrok\WatsonBridge\WatsonBridgeServiceProvider::class,
]

You can also add the following aliases to you app.php file:

'aliases' => [
    ...
    'Bridge'      => FindBrok\WatsonBridge\Facades\Bridge::class,
    'BridgeStack' => FindBrok\WatsonBridge\Facades\BridgeStack::class,
    'Carpenter'   => FindBrok\WatsonBridge\Facades\Carpenter::class,
]

Now publish the config file:

$ php artisan vendor:publish --tag=watson-api-bridge

You will now have a config file watson-bridge.php in your config directory. You may define in this config file your credentials, auth method to use, Watson Services and so on.

Services

The Laravel Integration gives you 3 service classes that are bound to the IoC.

  • FindBrok\WatsonBridge\Bridge (The actual Bridge class for making requests to Watson)
  • FindBrok\WatsonBridge\Support\Carpenter (Which can construct Bridge instances using your credentials and service URL)
  • FindBrok\WatsonBridge\Support\BridgeStack (Essentially a store where you can keep all Bridges you constructed and retrieve them back.)

Bridge

Bridge class will help you make requests to Watson API using the get, post, put, patch methods:

$response = $bridge->get('uri', $queryParams);

Carpenter

The Carpenter class can build any type of Bridge for you. Use the constructBridge method passing in the desired parameters like credentials name, service to use and auth method and so on and the Carpenter will.

$carpenter = app()->make(Carpenter::class);

$bridge = $carpenter->constructBridge('default', 'personality_insights');

Remember that your credentials names, services and auth methods are all defined in the watson-bridge.php config file.

BridgeStack

The BridgeStack is a great place to keep all your Bridges so that you can retrieve them anytime in your app. Use the mountBridge method to construct and keep any type of Bridge in the Stack.

$stack = app()->make(BridgeStack::class);

$stack->mountBridge('myPIBridge', 'default', 'personality_insights');
$stack->mountBridge('myTABridge', 'default', 'tradeoff_analytics');

// Now use the Bridges stored in the Stack.
$response = $stack->conjure('myPIBridge')->post('/v3/profile', $dataToPost);

The BridgeStack is essentially a Laravel Collection, thus you have access to all Collection methods.

Facades

If you are using Laravel version less than 5.4 you have access to 3 Facades for the 3 services Bridge, Carpenter and BridgeStack. Since Laravel 5.4 added automatic Facades you won't be needing those classes.

  • FindBrok\WatsonBridge\Facades\Bridge
  • FindBrok\WatsonBridge\Facades\BridgeStack
  • FindBrok\WatsonBridge\Facades\Carpenter

Remember that if you are resolving the Bridge directly from the IoC and not constructing it with the Carpenter class a default Bridge will be resolved for you using the default credentials and auth methods from your watson-bridge config.

Credits

Big Thanks to all developers who worked hard to create something amazing!

Creator

Percy Mamedy

Twitter: @PercyMamedy
GitHub: percymamedy