Last active
August 29, 2015 13:59
-
-
Save gravitano/10526411 to your computer and use it in GitHub Desktop.
Simple Twitter API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"require": { | |
"laravel/framework": "4.1.*", | |
"pingpong/twitter": "dev-master" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Twitter extends Eloquent | |
{ | |
protected $fillable = ['id_str', 'name', 'username', 'avatar']; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class TwitterController extends \BaseController { | |
/** | |
* Redirect. | |
* | |
* @return Response | |
*/ | |
public function redirect() | |
{ | |
return TwitterApi::authorize(route('twitter.callback')); | |
} | |
/** | |
* Callback. | |
* | |
* @return Response | |
*/ | |
public function callback() | |
{ | |
$callback = TwitterApi::getCallback(); | |
if($callback == 200) | |
{ | |
$user = TwitterApi::getCredentials(); | |
if($user->httpstatus == 200) | |
{ | |
$newTwitter = Twitter::firstOrNew([ | |
'id_str' => $user->id_str, | |
'name' => $user->name, | |
'username' => $user->screen_name | |
]); | |
$newTwitter->avatar = $user->profile_image_url; | |
$newTwitter->save(); | |
Session::put('user', $newTwitter->id); | |
return Redirect::home()->withFlashSuccess(__('twitter.success')); | |
} | |
return Redirect::home()->withFlashError(__('twitter.failed')); | |
} | |
return Redirect::home()->withFlashError(__('twitter.failed')); | |
} | |
/** | |
* Logout. | |
* | |
* @return Response | |
*/ | |
public function destroy() | |
{ | |
TwitterApi::destroy(); | |
Session::forget('user'); | |
return Redirect::home()->withFlashSuccess(__('twitter.destroyed')); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment