Creating A PHP Nexmo API Client Using Guzzle Web Service Client – Part 3.5

Creating A PHP Nexmo API Client Using Guzzle Web Service Client – Part 3.5
This is a small follow up on Part 3 in a series, you can read Part 1 here or Part 2 here.

In Part 3 of this series we completed API clients for Nexmo’s SMS and Number Insight APIs. Today I implemented the rest of the clients for Voice, Number Verify, and Developer APIs.

As I’ve hit on several times, using the Guzzle Web Service description way of developing an API client can save a lot of time. It took me a little less than an hour to finish adding support for these three sets of APIs. If I was writing every Guzzle client initialization and call individually it would have taken a lot longer I’m sure.

I also made the library available on Packagist for easy install. So now you can include and use this library in any of your projects with just a few simple steps:

  1. Install Composer:
    $ curl -sS https://getcomposer.org/installer | php
  2. Create/update your composer.json file to have at least:
    {
      "require": {
        "fillup/nexmo": "dev-master"
      }
    }
  3. Have composer install the library:
    $ composer install
    Loading composer repositories with package information
    Installing dependencies (including require-dev)
      - Installing psr/log (1.0.0)
        Loading from cache
    
      - Installing react/promise (v2.2.0)
        Loading from cache
    
      - Installing guzzlehttp/streams (3.0.0)
        Loading from cache
    
      - Installing guzzlehttp/ringphp (1.0.7)
        Loading from cache
    
      - Installing guzzlehttp/guzzle (5.2.0)
        Loading from cache
    
      - Installing guzzlehttp/log-subscriber (1.0.1)
        Loading from cache
    
      - Installing guzzlehttp/retry-subscriber (2.0.2)
        Loading from cache
    
      - Installing guzzlehttp/command (0.7.1)
        Loading from cache
    
      - Installing guzzlehttp/guzzle-services (0.5.0)
        Loading from cache
    
      - Installing fillup/nexmo (dev-master 39e829b)
        Cloning 39e829b4c859f5b64d99afec41d05ef49504b795
    
    Writing lock file
    Generating autoload files
  4. Include it in your code and use it (below is found in examples/sms.php):
    <?php
    /**
     * Include Composer autoloader
     */
    require_once __DIR__.'/../../vendor/autoload.php';
    
    /**
     * Import Sms client
     */
    use Nexmo\Sms;
    
    /**
     * Load config, expecting an array with:
     * api_key, api_secret, to, from, text
     */
    $config = include __DIR__.'/../../config-local.php';
    
    /**
     * Get an SMS client object
     */
    $sms = new Sms($config);
    
    /**
     * Now lets send a message
     */
    $results = $sms->send([
        'from' => $config['from'],
        'to' => $config['to'],
        'text' => $config['text'],
    ]);
    
    /**
     * Dump out results
     */
    print_r($results);

The clients available in this library are Nexmo\Developer, Nexmo\Insight, Nexmo\Sms, Nexmo\Verify, and Nexmo\Voice. They cover all of the outbound REST APIs but do not help with the inbound calls that Nexmo makes for certain APIs. I considered writing some kind of client to help there but since every application may handle inbound requests differently and the request would be so simple I probably couldn’t add any value there.

Well, thats it for Part 3.5 of this series. In Part 4 we’ll start building tests for the client so we can look at a couple strategies for testing API clients.

Links in this article:

Phillip Shipley avatar
About Phillip Shipley
Phillip has been a coder longer than he'd like to admit. Most of his work has been hacking on integrations and API development. Take everything he says with a bucket of salt.
comments powered by Disqus