Archive for the ‘twitter’ tag
Verifying Twitter account credentials using Symfony validator in PHP
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(&$value, &$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.
Innovative “Down for Scheduled Maintenance” landing pages
I was trying to visit Twitter’s website, but it was down for a planned maintenance. Here is the screenshot of it’s landing page. Very creative and funny too! It definitely made me smile. It definitely helped me not to get frustrated with their down time. Very clever.

But once I found Twitter is down, I decided to use Hootsuite for updating my Twitter status. As a co-incidence, it was also down at the same time. Here is the screenshot of their landing page. I found this is cleverer than Twitter’s. This is not only creative, but informative too. It definitely made me a little smarter than a moment ago. So who would bother about down-time, if you provide this kind of user-experience? Very very creative.

I think these are very clever design decisions to note down when I need to implement scheduled maintenance pages for my websites!