Geplaatst door op Mon, Jul 25 2016 15:47:04

In this blog post I'm going to a explain how you can use the Twitter API in a CakePHP application using webservices.

The implementation of the Twitter API is provided by our Twitter webservices plugin. It not only supports the regular API methods but even allows you to use their statuses streaming API.

Setting it up the plugin

Installing the plugin

To start you will need to install the plugin using Composer:

composer require cvo-technologies/cakephp-twitter

This will automatically install the Webservices plugin which provides the integration with CakePHP.

Now you will need to enable the two plugins. If you're already using a webservice implementation in your project then you will only need to load our Twitter implementation and not the Webservice plugin itself.

bin/cake plugin load Muffin/Webservices --bootstrap
bin/cake plugin load CvoTechnologies/Twitter

Configuring the webservice

Add the following to your Datasources section of your application's config.

        'twitter' => [
            'className' => 'Muffin\Webservice\Connection',
            'service' => 'CvoTechnologies/Twitter.Twitter',
            'consumerKey' => '',
            'consumerSecret' => '',
            'oauthToken' => '',
            'oauthSecret' => ''
        ]

Make sure to change the consumerKeyconsumerSecret. The oauthToken and oauthSecret can be left empty.

Using the Twitter implementation

In a controller

This is how you can use the implementation in a controller.

namespace App\Controller;

class StatusesController extends AppController
{
    public function beforeFilter(Event $event)
    {
        $this->loadModel('CvoTechnologies/Twitter.Statuses', 'Endpoint');
    }

    public function index()
    {
        $statuses = $this->Statuses->find()->where([
            'screen_name' => 'CVOTechnologies',
        ]);

        $this->set('statuses', $statuses);
    }
}

Examples

Requesting statuses posted by a user

$this->Statuses->find()->where([
    'screen_name' => 'CakePHP',
]);

Posting a status

$this->Statuses->save($this->Statuses->newEntity([
    'text' => 'Hello world!',
]));

0 Comments

Add new comment