Tuesday, July 8, 2014

How to Post tweet from Symfony2 application



This post will guide you to build simple twitter service for Symfony2 application using Codebird-php library,  So I decided to write one simple post to use this cool library in Symfony2.


A – Create twitter application

Creating a Twitter App is easy (see below steps)

A-1 : Go to https://dev.twitter.com/apps and login with your account.

A-2 : Click 'Create a new application' button.

A-3 : Complete the required fields and press 'Create your Twitter application'.

A-4 : Open 'Settings' TAB and set Application Type to 'Read and Write'. Then press 'Update this Twitter application's settings'

A-5 : Go to Details TAB and press 'Create my access token' button

A-6 : Go to oAuth tool TAB and get your access tokens

After creating the Twitter App, you will get following credentials to communicate using Twitter API.

1. ConsumerKey
2. ConsumerSecret
3. AccessToken
4. AccessTokenSecret


B – Install twitter library “Codebird-php” in Symfony2 project
Execute “php composer.phar require jublonet/codebird-php:dev-master” in root of your Symfony2 project directory, It will install codebird-php library in your project.



C – Add twitter applicatoin tokens and keys in parameter.yml
Add following parameters under “parameters:”
#app/config/parameters.yml
parameters:
    hm_twitterApiKey: "your-api-key"
    hm_twitterApiSecret: "your-api-secret"
    hm_twitterApiToken: "your-api-token"
    hm_twitterApiTokenSecret: "your-api-token-secret"


D – Create and register twitter service
Now, let's create service. It will allow you to send tweet to twitter from any controller or any other place of your choice.

D-1 : Create service class, with suitable namespace
<?php

    namespace Rm\DemoBundle\Services;

    use Codebird\Codebird;

    /**
     * Service to handle twitter post
     *
     * @author Rajesh Meniya
     */
    class TwitterService
    {
        /**
         * Service container
         *
         * @var container
         */
         protected $container;

        /**
         * construct
         */
        public function __construct($container)
        {
            $this->container = $container;
        }

        public function postTweet($status)
        {
            Codebird::setConsumerKey(
                $this->container->getParameter('icu_twitterApiKey'),
                $this->container->getParameter('icu_twitterApiSecret'));
            
            $cb = Codebird::getInstance();
            
            $cb->setToken(
                $this->container->getParameter('icu_twitterApiToken'),
                $this->container->getParameter('icu_twitterApiTokenSecret'));
                
            $reply = $cb->statuses_update('status=' . $status);
            
            return $reply;
        }
    }



D-2 : Register your service in your services.yml file
#src/Rm/DemoBundle/Resources/config/services.yml
services:
    rm.twitter:
        class: Rm\DemoBundle\Services\TwitterService
        arguments:
            container: “@service_container”

EPost tweet using your service.

Now you can use your twitter service in your application to send tweet, for example your can post tweet from your controller action like :

    $tweetText = "My new status from symfony application, just for test";
    $result = $this->get('hmgmt.twitter')->postTweet($tweetText);


Ref link: