Aditya Kothadiya's Blog

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

Verifying Twitter account credentials using Symfony validator in PHP

Aditya April 1st

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.

Posted in ProgrammingTagged with , , , ,

View Comments to 'Verifying Twitter account credentials using Symfony validator in PHP'

Subscribe to comments with RSS or TrackBack to 'Verifying Twitter account credentials using Symfony validator in PHP'.

  1. Great post Adi. I like your APICall method. Looks very generic and can be used for any Web Service API calls.

    Swapnil.

    Swapnil

    1 Apr 09 at 10:59 pm

  2. Thanks Swapnil! Yeah, the purpose of APICall was to make it usable for all different Twitter API calls, or even other web service calls.

    aditya

    2 Apr 09 at 10:24 am

  3. Any chance you can provide a link to download the source files (actions, lib, templates, etc) for this tutorial?

    Thanks

    Hardeep Khehra

    25 Jun 09 at 6:01 am

  4. Unfortunately I cannot provide all source files because there is additional code. This was not created for tutorial per say, but it was taken from my current live example. So to provide downloadeable files, I'll need to modify my files, and I'm limited with time bandwidth.

    aditya

    25 Jun 09 at 11:50 pm

  5. I like your APICall method. Looks very generic and can be used for any Web Service API calls.

  6. I like your APICall method. Looks very generic and can be used for any Web Service API calls.

  7. I like your APICall method. Looks very generic and can be used for any Web Service API calls.

Leave a Reply

blog comments powered by Disqus