<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Aditya Kothadiya&#039;s Blog &#187; validator</title>
	<atom:link href="http://adityakothadiya.com/tag/validator/feed/" rel="self" type="application/rss+xml" />
	<link>http://adityakothadiya.com</link>
	<description>Entrepreneurship, programming, design, productivity, books, philosophy and more.</description>
	<lastBuildDate>Sun, 16 May 2010 16:20:16 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Verifying Twitter account credentials using Symfony validator in PHP</title>
		<link>http://adityakothadiya.com/2009/04/verifying-twitter-account-credentials-using-symfony-validator-in-php/</link>
		<comments>http://adityakothadiya.com/2009/04/verifying-twitter-account-credentials-using-symfony-validator-in-php/#comments</comments>
		<pubDate>Thu, 02 Apr 2009 04:47:31 +0000</pubDate>
		<dc:creator>Aditya</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[login]]></category>
		<category><![CDATA[php]]></category>
		<category><![CDATA[symfony]]></category>
		<category><![CDATA[twitter]]></category>
		<category><![CDATA[validator]]></category>

		<guid isPermaLink="false">http://adityakothadiya.com/?p=418</guid>
		<description><![CDATA[If you&#8217;re using Symfony &#8211; a PHP&#8217;s MVC framework to develop your web application, and if your application interacts with Twitter, and if you need Twitter user&#8217;s credentials to access his or her data from Twitter, then this might be useful to you.
Here is a simple Twitter class -

&#60;?php
class Twitter {
private $credentials;

function Twitter($username, $password) {
$this-&#62;credentials [...]]]></description>
			<content:encoded><![CDATA[<p>If you&#8217;re using <strong><a href="http://www.symfony-project.org" target="_blank">Symfony</a> &#8211; a PHP&#8217;s MVC framework</strong> to develop your web application, and if your application interacts with <a href="http://twitter.com" target="_blank">Twitter</a>, and if you need Twitter user&#8217;s credentials to access his or her data from Twitter, then this might be useful to you.</p>
<p><strong>Here is a simple Twitter class -</strong></p>
<pre class="brush: php;">
&lt;?php
class Twitter {
private $credentials;

function Twitter($username, $password) {
$this-&gt;credentials = sprintf(&quot;%s:%s&quot;, $username, $password);
}

function verifyCredentials($format = NULL) {
$api_call = sprintf(&quot;http://twitter.com/account/verify_credentials%s&quot;, ($format != NULL) ? sprintf(&quot;.%s&quot;, $format) : NULL);
return $this-&gt;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-&gt;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;
}
}
?&gt;
</pre>
<p><strong>Then here is the simple action to handle form &#8211; </strong></p>
<pre class="brush: php;">
public function executeTwitterLogin()
{
if ($this-&gt;getRequest()-&gt;getMethod() == sfRequest::POST)
{
$username = $this-&gt;getRequestParameter('username');
$password =  $this-&gt;getRequestParameter('password');

// Your business logic here...
}
}
</pre>
<p><strong>Here is the action to handle error for TwitterLogin action &#8211; </strong></p>
<pre class="brush: php;">
public function handleTwitterLogin()
{
return sfView::SUCCESS;
}
</pre>
<p><strong>Here is a simple template with the form asking for the Twitter login credentials -</strong></p>
<pre class="brush: php;">
&lt;?php use_helper('Validation', 'Form') ?&gt;

&lt;?php echo form_tag('@twitter_login') ?&gt;

&lt;p&gt;Please enter your Twitter account's login credentials below.&lt;/p&gt;

&lt;?php echo form_error('username') ?&gt;
&lt;label for=&quot;username&quot;&gt;Twitter Username:&lt;/label&gt;
&lt;?php echo input_tag('username', $sf_params-&gt;get('username')) ?&gt;

&lt;?php echo form_error('password') ?&gt;
&lt;label for=&quot;password&quot;&gt;Twitter Password:&lt;/label&gt;
&lt;?php echo input_password_tag('password') ?&gt;

&lt;?php echo submit_tag('Login') ?&gt;
&lt;/form&gt;
</pre>
<p><strong>Here is the twitterLogin.yml validator file &#8211; </strong></p>
<pre class="brush: php;">
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
</pre>
<p><strong><br />
And finally, here is the custom class which is extended from sfvalidator for authenticating Twitter login details.</strong></p>
<pre class="brush: php;">
&lt;?php
class myTwitterLoginValidator extends sfValidator
{
public function initialize($context, $parameters = null)
{
// initialize parent
parent::initialize($context);

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

return true;
}

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

$username = $value;

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

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

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

$error = $this-&gt;getParameter('error');
return false;
}
}
?&gt;
</pre>
<p>I hope you&#8217;ll find this useful. Let me know in the comments section if you see any issue while using it.</p>
]]></content:encoded>
			<wfw:commentRss>http://adityakothadiya.com/2009/04/verifying-twitter-account-credentials-using-symfony-validator-in-php/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
