Revision #4 has been created by lucifurious on Jun 12, 2009, 12:44:27 AM with the memo:
Changed code formatting
« previous (#3) next (#5) »
Changes
Title
unchanged
How To Connect With Twitter From Yii
Category
unchanged
Tutorials
Yii version
unchanged
Tags
unchanged
Content
changed
[...]
=====================
Now that you've told Twitter about your application, we need to tell Yii to create the Twitter component. This is done in the main.php configuration file.
If you have not yet configured your main.php for the psYiiExtension library, do so now by following the installation instructions.
Add the following code to your 'components' array:
<pre>
```php
// Twitter API
'twitter' => array(
'class' => 'pogostick.components.twitter.CPSTwitterApi',[...]
'format' => 'array',
),
</pre>```
Be sure to replace the bold text items with the information you entered and obtained when you created your Twitter application in step #1.[...]
In either case, you'll need to obtain an access token from the Twitter OAuth server. This is done by calling the <b>authorize</b> URL.
In the code sample below is ALL the code from my views/site/twitter.php (also my callback URL).:
### vews/site/twitter.php ###
<code>
<
```php
<?php
$this-
>>pageTitle=Yii::app()-
>>name . ' - Attach to Twitter';
?
>>
<<? if ( ! Yii::app()-
>>twitter-
>>isAuthorized ) { ?
>
<h1>>
<h1>Link your Twitter Account!
</h1></h1>
<<div class=
""yiiForm
">
<">
<?php echo CHtml::form(); ?
>>
<<div class=
"simple">"simple">
Click the button below to be taken to
<<a href=
""http://www.twitter.com/
"" target=_blank
>>Twitter
</a></a> to link your account.
<br />
<br />
<br /><br />
<br />
<br />
<<a href=
"<"<?=Yii::app()-
>>twitter-
>>getAuthorizeUrl()?
>"><>"><img src=
""/images/twitter_sign.png
"" border=
"0" alt="Sign in with Twitter" /></a>
</div>
</form>
</div><"0" alt="Sign in with Twitter" /></a>
</div>
</form>
</div><!-- yiiForm --
>
<>
<? } else { ?
>
<h1>>
<h1>Linked with Twitter!
</h1></h1>
<<div class=
""yiiForm
">
<">
<?php echo CHtml::form(); ?
>>
<<div class=
"simple">"simple">
Your account is currently linked to your Twitter account
<b><<b><?= Yii::app()-
>>twitter-
>>screenName ?
>>.
<br />
<br />
<br />
</div>
</form>
</div><!-- yiiForm -->
<? } ?>
</pre><br />
<br />
<br />
</div>
</form>
</div><!-- yiiForm -->
<? } ?>
```
Capturing the Access Token[...]
It is up to you to store this someplace. The library places the token in a user session state. You can retrieve the token via:
<pre>
```php
if ( Yii::app()->twitter->isAuthorized )
$_arToken = Yii::app()->twitter-><b>getToken()</b>;
else
echo 'User is not authorized';
</pre>```
The token is actually an array with two (or more once authenticated) elements. After authorization, the array contains the following elements:[...]
Here's the code:
<pre>
```php
<?php
/**[...]
* @license http://www.pogostick.com/license/
*/
/**
* CPS provides
*[...]
}
}
}
</pre>}
```
Now my site allows multiple identities (i.e. Facebook, Twitter, Flickr, etc.) and I needed a single management point. I have a table that stores the token information for each platform.[...]
This looks up my tokens from the database and pre-loads them into the various platform objects like Twitter:
<pre>
```php
Yii::app()->twitter->loadData( 'user_id', 'screen_name', is_authenticated, array( 'oauth_token' => oauth_token, 'oauth_token_secret' => oauth_token_secret ) );
</pre>```
Once you make that call, you're all set to use the Twitter API![...]
So, here's a sample call I made to get mentions of my Twitter account:
<pre>
```php
$_arResults = Yii::app()->twitter->getMentions();
echo var_export( $_arResults, true );
</pre>```
Produces:
<pre>
```php
array (
0 =>[...]
<b>...</b>
)
</pre>```
# Wrapping it up! #
So you should be well on your way to geeking with Twitter and Yii now. If I've left anything out or if you're confused or my writing sucks, please let me know. I love feedback! jablan 'at' pogostick.com or PM me here.