Note: I'm migrating from gonzalo123.com to here. When I finish I'll swap the DNS to here. The "official" blog will be always gonzalo123.com

      How to configure Symfony's Service Container to use Twitter API

      Keeping on with the series about Symfony’s Services container (another posts here and here), now we will use the service container to use Twitter API from a service.

      To use Twitter API we need to handle http requests. I’ve written several post about http request with PHP (example1, example2), but today we will use one amazing library to build clients: Guzzle. Guzzle is amazing. We can easily build a Twitter client with it. There’s one example is its landing page:

      <?php
      $client = new Client('https://api.twitter.com/{version}', array('version' => '1.1'));
      $oauth  = new OauthPlugin(array(
          'consumer_key'    => '***',
          'consumer_secret' => '***',
          'token'           => '***',
          'token_secret'    => '***'
      ));
      $client->addSubscriber($oauth);
       
      echo $client->get('/statuses/user_timeline.json')->send()->getBody();
      

      If we are working within a Symfony2 application or a PHP application that uses the Symfony’s Dependency injection container component you can easily integrate this simple script in the service container. I will show you the way that I use to do it. Let’s start:

      The idea is simple. First we include guzzle within our composer.json and execute composer update:

      "require": {
          "guzzle/guzzle":"dev-master"
      }
      

      Then we will create two files, one to store our Twitter credentials and another one to configure the service container:

      # twitter.conf.yml
      parameters:
        twitter.baseurl: https://api.twitter.com/1.1
       
        twitter.config:
          consumer_key: ***
          consumer_secret: ***
          token: ***
          token_secret: ***
      
      # twitter.yml
      parameters:
        class.guzzle.response: Guzzle\Http\Message\Response
        class.guzzle.client: Guzzle\Http\Client
        class.guzzle.oauthplugin: Guzzle\Plugin\Oauth\OauthPlugin
       
      services:
        guzzle.twitter.client:
          class: %class.guzzle.client%
          arguments: [%twitter.baseurl%]
          calls:
            - [addSubscriber, [@guzzle.twitter.oauthplugin]]
       
        guzzle.twitter.oauthplugin:
          class: %class.guzzle.oauthplu
      

      And finally we include those files in our services.yml:

      # services.yml
      imports:
      - { resource: twitter.conf.yml }
      - { resource: twitter.yml }
      

      And that’s all. Now we can use the service without problems:

      <?php
       
      namespace Gonzalo123\AppBundle\Controller;
       
      use Symfony\Bundle\FrameworkBundle\Controller\Controller;
       
      class DefaultController extends Controller
      {
          public function indexAction($name)
          {
              $twitterClient = $this->container->get('guzzle.twitter.client');
              $status = $twitterClient->get('statuses/user_timeline.json')
                   ->send()->getBody();
       
              return $this->render('AppBundle:Default:index.html.twig', array(
                  'status' => $status
              ));
          }
      }
      

      comments powered by Disqus