コード例 #1
0
 /**
  * the default Authentication route any new request to twitter has to go throw this action first
  * @param string $redirectRoute the route to redirect to after connecting to twitter
  * @param string $popup if set to "yes" the twitter action will attempt to close a popup and redirect it is parent instade of redirect
  */
 public function indexAction($redirectRoute, $popup = 'no')
 {
     //get the session object
     $session = $this->getRequest()->getSession();
     //get the translator
     $translator = $this->get('translator');
     //get the container
     $container = $this->container;
     /* Build TwitterOAuth object with client credentials. */
     $connection = new TwitterOAuth($container->getParameter('consumer_key'), $container->getParameter('consumer_secret'));
     /* Get temporary credentials. */
     $request_token = @$connection->getRequestToken($this->generateUrl('twitter_callback', array(), TRUE));
     /* If last connection failed don't display authorization link. */
     switch ($connection->http_code) {
         case 200:
             /* Save temporary credentials to session. */
             $session->set('oauth_token', $request_token['oauth_token']);
             $session->set('oauth_token_secret', $request_token['oauth_token_secret']);
             $session->set('redirectRoute', $redirectRoute);
             //check if we will set the popup flag
             if ($popup == 'yes') {
                 //set the flag
                 $session->set('twitterPopup', TRUE);
             }
             /* Build authorize URL and redirect user to Twitter. */
             $url = $connection->getAuthorizeURL($request_token['oauth_token']);
             return $this->redirect($url);
         default:
             /* Show notification if something went wrong. */
             $session->clear();
             return new Response($translator->trans('twitter connection error') . ' <a href="' . $this->generateUrl('twitter_authentication', array('redirectRoute' => $redirectRoute), TRUE) . '">' . $translator->trans('try again') . '</a>');
     }
 }