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

Latest commit

 

History

History
122 lines (99 loc) · 3.33 KB

2015-09-11-connecting-to-a-web-service.markdown

File metadata and controls

122 lines (99 loc) · 3.33 KB
layout title date comments categories
post
Connecting to a remote web service
2015-09-11 12:21:14 +0100
true
cakephp
cakephp3

First things first

So the first thing you'll need is the Webservice plugin.

composer require muffin/webservice:dev-master

Then you need to load the plugin in your config/bootstrap.php and tell the ConnectionManager to consume your config.

<?php
Plugin::load('Muffin/Webservice');
ConnectionManager::config(Configure::consume('Webservices'));

Then be sure to add the configuration for your service to the config/app.php file. This means you need to add another array key to your config, it should contain something like the following.

<?php
return [
	// The rest of your app config
	
	'Webservices' => [
	    'Twitter' => [
	        'className' => 'Muffin\Webservice\Connection',
	        'service' => 'App\Lib\Twitter\Driver\Twitter',
	        'username' => env('GITHUB_USERNAME'),
	        'password' => env('GITHUB_PASSWORD'),
	    ]
	]
]; // This is the end of your config/app.php file, so the key sits on the 
   // same array level as 'Session' above

I'll talk through the various config options here.

className

This is a proxy class which will pass method calls through to your own driver class.

service

This is your driver class where you'll write your code and connect to the service.

username/password

Any login credentials you need will have to be included.

Creating your driver class

So you need to create a new driver class, which uses the Webservice plugin as a base. For my example, I've created my Twitter driver class in src/Lib/Twitter/Driver/Twitter.php which extends Muffin\Webservice\AbstractDriver, and I've implemented the stub method, which is just initialize, to setup the client you want to use.

<?php
// src/Lib/Twitter/Driver/Twitter.php
namespace App\Lib\Twitter\Driver;

use Cake\Network\Http\Client;
use Muffin\Webservice\AbstractDriver;

class Twitter extends AbstractDriver
{
	public function initialize()
	{
		// Let's use the CakePHP Http client class
		$this->_client = new \Cake\Network\Http\Client();
	}
	
	// etc

There is a gotcha here!
You will need to implement some methods from Cake\Database\Connection into your driver class so that DebugKit can interact with your driver. These methods can be copied and pasted from the Connection class.

  • configName()
  • logQueries()
  • logger()

Creating your method

So assuming we want to get some tweets, we can create a method for that inside our driver class.

<?php
public function tweets()
{
    // Do some setup for Twitter
    return $this->_client->post($url, $dataArray);
}

This will use the CakePHP Http Client to post to a Twitter url and return the response.

Using your new driver

So now we need to actually get the data to play with. In our controller we need to instantiate the connection and call our new method.

<?php
// ExamplesController.php
public function getTweets()
{
    $connection = ConnectionManager::get('Twitter');
    $tweets = $connection->tweets();
    $this->set('tweets', $tweets);
}

You must make sure that the name of the connection you give to ConnectionManager matches the name you used in your app.php config setup.

That's it!

Job done! Have a brew.