Aditya Kothadiya's Blog

Entrepreneurship, programming, design, productivity, books, philosophy and more.

Archive for the ‘validator’ tag

Verifying Twitter account credentials using Symfony validator in PHP

View Comments

If you’re using Symfony – a PHP’s MVC framework to develop your web application, and if your application interacts with Twitter, and if you need Twitter user’s credentials to access his or her data from Twitter, then this might be useful to you.

Here is a simple Twitter class -

<?php
class Twitter {
private $credentials;

function Twitter($username, $password) {
$this->credentials = sprintf("%s:%s", $username, $password);
}

function verifyCredentials($format = NULL) {
$api_call = sprintf("http://twitter.com/account/verify_credentials%s", ($format != NULL) ? sprintf(".%s", $format) : NULL);
return $this->APICall($api_call, true);
}

private function APICall($api_url, $require_credentials = false, $http_post = false) {
$curl_handle = curl_init();
curl_setopt($curl_handle, CURLOPT_URL, $api_url);

if ($require_credentials) {
curl_setopt($curl_handle, CURLOPT_USERPWD, $this->credentials);

}
if ($http_post) {
curl_setopt($curl_handle, CURLOPT_POST, true);
}
curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, TRUE);
$twitter_data = curl_exec($curl_handle);

curl_close($curl_handle);
return $twitter_data;
}
}
?>

Then here is the simple action to handle form –

public function executeTwitterLogin()
{
if ($this->getRequest()->getMethod() == sfRequest::POST)
{
$username = $this->getRequestParameter('username');
$password =  $this->getRequestParameter('password');

// Your business logic here...
}
}

Here is the action to handle error for TwitterLogin action –

public function handleTwitterLogin()
{
return sfView::SUCCESS;
}

Here is a simple template with the form asking for the Twitter login credentials -

<?php use_helper('Validation', 'Form') ?>

<?php echo form_tag('@twitter_login') ?>

<p>Please enter your Twitter account's login credentials below.</p>

<?php echo form_error('username') ?>
<label for="username">Twitter Username:</label>
<?php echo input_tag('username', $sf_params->get('username')) ?>

<?php echo form_error('password') ?>
<label for="password">Twitter Password:</label>
<?php echo input_password_tag('password') ?>

<?php echo submit_tag('Login') ?>
</form>

Here is the twitterLogin.yml validator file –

methods:
post: [username, password]

names:
username:
required:     true
required_msg: Your Twitter Username is required.
validators:   userValidator

password:
required:     true
required_msg: Your Twitter Password is required.

userValidator:
class:        twitterLoginValidator
param:
password:    password


And finally, here is the custom class which is extended from sfvalidator for authenticating Twitter login details.

<?php
class myTwitterLoginValidator extends sfValidator
{
public function initialize($context, $parameters = null)
{
// initialize parent
parent::initialize($context);

// set defaults
$this->setParameter('error', 'Your Twitter account credentials are incorrect.');
$this->getParameterHolder()->add($parameters);

return true;
}

public function execute(&amp;$value, &amp;$error)
{
$password_param = $this->getParameter('password');
$password = $this->getContext()->getRequest()->getParameter($password_param);

$username = $value;

// Verify Twitter Credentials
$twitter = new Twitter($username, $password);
$resp_xml = $twitter->verifyCredentials('xml');
$resp_xml = simplexml_load_string($resp_xml);

$resp_username = $resp_xml->screen_name;
$verified = strcasecmp($username, $resp_username); // returns 0 if they are equal

if ($verified === 0)
return true;

$error = $this->getParameter('error');
return false;
}
}
?>

I hope you’ll find this useful. Let me know in the comments section if you see any issue while using it.

Written by Aditya

April 1st, 2009 at 8:47 pm

Posted in Programming

Tagged with , , , ,