Using Twitter Api (1.1) With Yii

Hi,

I was having a problem to use Twitter API with Yii. After trying a lot of things, I came up with this solution.

I hope it will be useful to someone

First:

Download this extension "yiiframework dot com/extension/twitter"

Install as the author say.

Second:

Download the latest version of abraham/twitteroauth "github dot com/abraham/twitteroauth"

In that zip, go to twitteroauth-master/twitteroauth and copy the two files to extensios/yiitwitteroauth

You would be asked to overwrite the files.

Third:

In extensions/twitteroauth/OAuth.php

Find:


 if (!class_exists('OAuthException')) {

  class OAuthException extends Exception {

    // pass

  }

} 

And replace with:


if(!class_exists('OAuthException',false)){

	class OAuthException extends Exception {

	  // pass

	}

}

In your controller find the new action actionIndex and replace


 

    $request_token = $twitter->getRequestToken();



With this:




 $callbackurl='YOUR_URL/controller/twitterCallBack'; // this is your callback url!!!!

     $request_token = $twitter->getRequestToken($callbackurl);	



*: I don’t know how to use your previous configured callbackurl in your config/main.php sorry!

With that done, you can start to make api calls.

So far, I’ve come with this example code and is working (not sure if it’s the best solution).





public function actionSearchtweets(){	

		

		if(Yii::app()->session['status']!='verified'){

			echo "Error Token no verificado"; // ??

		

		}else{	

		        $token=Yii::app()->session['access_token']['oauth_token']; // previous obtained token

			$secret= Yii::app()->session['access_token']['oauth_token_secret']; // previous obtained password

			$twitter = Yii::app()->twitter->getTwitterTokened($token,$secret);

			$result=$twitter->get('search/tweets', array('q' => "Mendoza")); // api call	   

			print_r($result);

		}

	}







If you don’t want to make all this by yourself. I have attached my working copy of the extension (Oh I’m a new user, can’t attach a file)

Thanks for your nice post. :) Its been too long for me i haven’t work with Twitter Outh. :D But i remember how typical that was to deal with Twitter Outh.

Anyways it would be nice if you can put this content as a article.then it would be great :)

I’ll try to put it as a article, but right now I’m too new to have that permission.

pd: really…really sorry for my english I’m from Argentina.

I did everything you said but I have an error Fatal error: Cannot redeclare class OAuthSignatureMethod_HMAC_SHA1 in C:\wamp\www\projectfolder\OAuth.php on line 120 do you had this error as well?