findbrok/php-watson-api-bridge

This package is abandoned and no longer maintained. No replacement package was suggested.

A simple PHP Wrapper around IBM Watson API

v1.1.2 2017-06-06 18:42 UTC

This package is auto-updated.

Last update: 2023-06-05 01:44:29 UTC


README

php-watson-api-bridge.png

PHP IBM Watson API Bridge

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

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