Difference between #3 and #4 of
How To Connect With Twitter From Yii

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>
 
&lt;
 
```php 
<
?php $this-&gt;>pageTitle=Yii::app()-&gt;>name . ' - Attach to Twitter'; ?&gt;> &lt;<? if ( ! Yii::app()-&gt;>twitter-&gt;>isAuthorized ) { ?&gt;
 
&lt;h1&gt;
>
 
<h1>
Link your Twitter Account!&lt;/h1&gt;</h1> &lt;<div class=&quot;"yiiForm&quot;&gt;
 
&lt;
">
 
<
?php echo CHtml::form(); ?&gt;> &lt;<div class=&quot;simple&quot;&gt;"simple"> Click the button below to be taken to &lt;<a href=&quot;"http://www.twitter.com/&quot;" target=_blank&gt;>Twitter&lt;/a&gt;</a> to link your account. &lt;br /&gt;
 
&lt;br /&gt;
 
&lt;br /&gt;
<br />
 
<br />
 
<br />
&lt;<a href=&quot;&lt;"<?=Yii::app()-&gt;>twitter-&gt;>getAuthorizeUrl()?&gt;&quot;&gt;&lt;>"><img src=&quot;"/images/twitter_sign.png&quot;" border=&quot;0&quot; alt=&quot;Sign in with Twitter&quot; /&gt;&lt;/a&gt;
 
&lt;/div&gt;
 
&lt;/form&gt;
 
&lt;/div&gt;&lt;
"0" alt="Sign in with Twitter" /></a>
 
</div>
 
</form>
 
</div><
!-- yiiForm --&gt;
 
&lt;
>
 
<
? } else { ?&gt;
 
&lt;h1&gt;
>
 
<h1>
Linked with Twitter!&lt;/h1&gt;</h1> &lt;<div class=&quot;"yiiForm&quot;&gt;
 
&lt;
">
 
<
?php echo CHtml::form(); ?&gt;> &lt;<div class=&quot;simple&quot;&gt;"simple"> Your account is currently linked to your Twitter account &lt;b&gt;&lt;<b><?= Yii::app()-&gt;>twitter-&gt;>screenName ?&gt;>. &lt;br /&gt;
 
&lt;br /&gt;
 
&lt;br /&gt;
 
&lt;/div&gt;
 
&lt;/form&gt;
 
&lt;/div&gt;&lt;!-- yiiForm --&gt;
 
&lt;? } ?&gt;
 
</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.
4 1
12 followers
Viewed: 26 013 times
Version: 1.1
Category: Tutorials
Tags: twitter
Written by: lucifurious
Last updated by: wei
Created on: Jun 11, 2009
Last updated: 7 years ago
Update Article

Revisions

View all history