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:


17 comments:

  1. Well post, Thanks for sharing this to our vision. In recent day’s customer relationship play vital role to get good platform in business industry, Sales force crm tool helps you to maintain your customer relationship enhancement.
    Regards,
    Salesforce training in Chennai|Salesforce training center in Chennai|Salesforce training in velachery

    ReplyDelete
  2. Good Post! Thank you so much for sharing this pretty post, it was so good to read and useful to improve my knowledge as updated one, keep blogging…
    Regards,
    cognos Training in Chennai|Best COGNOS Training Institute in Chennai|cognos Certification

    ReplyDelete
  3. Excellent Post, I welcome your interest about to post blogs. It will help many of them to update their skills in their interesting field.
    Regards,
    python training chennai|Python Course in Chennai|FITA Academy Chennai|FITA Academy reviews

    ReplyDelete
  4. how can we upload video using codebird with symfony framework

    ReplyDelete
  5. Useful information.I am actual blessed to read this article.thanks for giving us this advantageous information.I acknowledge this post.and I would like bookmark this post.Thanks
    angularjs Training in bangalore

    angularjs Training in btm

    angularjs Training in electronic-city

    angularjs online Training

    angularjs Training in marathahalli

    angularjs interview questions and answers

    ReplyDelete
  6. Thank you so much for a well written, easy to understand article on this. It can get really confusing when trying to explain it – but you did a great job. Thank you!
    python training in rajajinagar
    Python training in bangalore
    Python training in usa

    ReplyDelete
  7. Your info is really amazing with impressive content..Excellent blog with informative concept. Really I feel happy to see this useful blog, Thanks for sharing such a nice blog..
    If you are looking for any Data science Related information please visit our website best course for data science page!

    ReplyDelete
  8. JavaScript, often abbreviated as JS, is a programming language that conforms to the ECMAScript specification. JavaScript is high-level, often just-in-time compiled, and multi-paradigm. It has curly-bracket syntax, dynamic typing, prototype-based object-orientation, and first-class functionsJava training in chennai

    python training in chennai

    web designing and development training in chennai

    selenium training in chennai

    digital-marketing training in chennai

    ReplyDelete
  9. Great post! I am actually getting ready to across this information, It’s very helpful for this blog.Also great with all of the valuable information you have Keep up the good work you are doing well.

       AWS training in chennai      AWS Course in Chennai    |    AWS Certification Cost in Chennai  

    ReplyDelete
  10. Hi, Honestly it is a nice blog.
    Looking for more such blogs from you.
    Enstyle Planners Event management company not only saves time and money but also ensures that your event fits the budget and meets your preferences; thus making it a less stressful affair.
    Best Marriage Event Organisers In Hyderabad | Best Marriage Event Organisers In Hyderabad | Wedding Event Management Companies In Hyderabad | Best Wedding Planners In Hyderabad | Top Wedding Planners In Hyderabad | Marriage Event Organisers In Hyderabad | Marriage Event Management In Hyderabad | Wedding Mehendi Artists In Hyderabad

    ReplyDelete