PHP code example of friendsofsymfony / twitter-bundle
1. Go to this page and download the library: Download friendsofsymfony/twitter-bundle library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
friendsofsymfony / twitter-bundle example snippets
php
// src/Acme/YourBundle/Entity/User.php
/**
* @var string
*/
protected $twitterID;
/**
* @var string
*/
protected $twitter_username;
/**
* Set twitterID
*
* @param string $twitterID
*/
public function setTwitterID($twitterID)
{
$this->twitterID = $twitterID;
$this->setUsername($twitterID);
$this->salt = '';
}
/**
* Get twitterID
*
* @return string
*/
public function getTwitterID()
{
return $this->twitterID;
}
/**
* Set twitter_username
*
* @param string $twitterUsername
*/
public function setTwitterUsername($twitterUsername)
{
$this->twitter_username = $twitterUsername;
}
/**
* Get twitter_username
*
* @return string
*/
public function getTwitterUsername()
{
return $this->twitter_username;
}
php
// src/Acme/YourBundle/Security/User/Provider/TwitterUserProvider.php
namespace Acme\YourBundle\Security\User\Provider;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\HttpFoundation\Session;
use \TwitterOAuth;
use FOS\UserBundle\Entity\UserManager;
use Symfony\Component\Validator\Validator;
class TwitterUserProvider implements UserProviderInterface
{
/**
* @var \Twitter
*/
protected $twitter_oauth;
protected $userManager;
protected $validator;
protected $session;
public function __construct(TwitterOAuth $twitter_oauth, UserManager $userManager,Validator $validator, Session $session)
{
$this->twitter_oauth = $twitter_oauth;
$this->userManager = $userManager;
$this->validator = $validator;
$this->session = $session;
}
public function supportsClass($class)
{
return $this->userManager->supportsClass($class);
}
public function findUserByTwitterId($twitterID)
{
return $this->userManager->findUserBy(array('twitterID' => $twitterID));
}
public function loadUserByUsername($username)
{
$user = $this->findUserByTwitterId($username);
$this->twitter_oauth->setOAuthToken($this->session->get('access_token'), $this->session->get('access_token_secret'));
try {
$info = $this->twitter_oauth->get('account/verify_credentials');
} catch (Exception $e) {
$info = null;
}
if (!empty($info)) {
if (empty($user)) {
$user = $this->userManager->createUser();
$user->setEnabled(true);
$user->setPassword('');
$user->setAlgorithm('');
}
$username = $info->screen_name;
$user->setTwitterID($info->id);
$user->setTwitterUsername($username);
$user->setEmail('');
$user->setFirstname($info->name);
$this->userManager->updateUser($user);
}
if (empty($user)) {
throw new UsernameNotFoundException('The user is not authenticated on twitter');
}
return $user;
}
public function refreshUser(UserInterface $user)
{
if (!$this->supportsClass(get_class($user)) || !$user->getTwitterID()) {
throw new UnsupportedUserException(sprintf('Instances of "%s" are not supported.', get_class($user)));
}
return $this->loadUserByUsername($user->getTwitterID());
}
}
php
// src/Acme/YourBundle/Controller/DefaultController.php
/**
* @Route("/connectTwitter", name="connect_twitter")
*
*/
public function connectTwitterAction()
{
$request = $this->get('request');
$twitter = $this->get('fos_twitter.service');
$authURL = $twitter->getLoginUrl($request);
$response = new RedirectResponse($authURL);
return $response;
}