Signin Sina Weibo Open Platform, get App Key and App Secret, reference PHP SDK. The main principle is after authority on Sina Weibo, store uid, access_token and other information in local database. Determine the access_token at user next signin.
Create users table
I prefer use migration, run in terminal:
$ php artisan migrate:make create_users_table
File: app/database/migrations/2014_07_17_0000_create_users_table.php
<?php
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;
class CreateUsersTable extends Migration {
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('users', function(Blueprint $table)
{
$table->increments('id');
$table->string('email');
$table->string('password');
$table->string('username', 60)->nullable();
// ...
$table->timestamp('deleted_at')->nullable();
$table->timestamp('activated_at')->nullable();
$table->timestamp('signin_at')->nullable();
$table->timestamps();
});
}
/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('users');
}
}
Download and extracting PHP SDK, rename floder as api and place at app/api
├── app │ ├── api │ │ ├── callback.php │ │ ├── config.php │ │ ├── index.php │ │ ├── saetv2.ex.class.php │ │ └── weibolist.php │ ├── commands │ ├── config │ ├── controllers │ ├── database │ ├── lang │ ├── ... ├── bootstrap ├── public ├── readme ├── vendor ├── ... ...
File: app/api/config.php
Fill in your App Key, App Secret and set callback url.
<?php
header('Content-Type: text/html; charset=UTF-8');
define( "WB_AKEY" , 'xxxxxxxxxx' );
define( "WB_SKEY" , 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx' );
define( "WB_CALLBACK_URL" , route('oauth-signup') );
File: route.php
// ...
/*
|--------------------------------------------------------------------------
| Basic Competence (Signin and Signup Routes)
|--------------------------------------------------------------------------
|
*/
Route::group(array('prefix' => 'auth'), function () {
$Authority = 'AuthorityController@';
// ...
# Route Group
Route::group(array('before' => 'guest'), function () use ($Authority) {
// ...
# Oauth Signup
Route::get('oauth-signup' , array('as' => 'oauth-signup' , 'uses' => $Authority.'getOauthSignup' ));
# Oauth Success
Route::get('oauth-success', array('as' => 'oauth-success' , 'uses' => $Authority.'getOauthSuccess', 'before' => 'auth' ));
// ...
});
});
// ...
File: AuthorityController.php
// ...
/**
* Action:Oauth 2.0 Signup
* @return Response
*/
public function getOauthSignup()
{
header("Content-type:text/html;charset=utf-8");
session_start();
include_once( app_path('api/config.php') );
include_once( app_path('api/saetv2.ex.class.php') );
$o = new SaeTOAuthV2( WB_AKEY , WB_SKEY );
if (isset($_REQUEST['code'])) {
$keys = array();
$keys['code'] = $_REQUEST['code'];
$keys['redirect_uri'] = WB_CALLBACK_URL;
try {
$token = $o->getAccessToken( 'code', $keys ) ;
} catch (OAuthException $e) {
}
}
if ($token) {
$_SESSION['token'] = $token;
setcookie( 'weibojs_'.$o->client_id, http_build_query($token) );
$c = new SaeTClientV2( WB_AKEY , WB_SKEY , $_SESSION['token']['access_token'] );
$ms = $c->home_timeline(); // Done
$uid_get = $c->get_uid();
$uid = $uid_get['uid'];
$user_message = $c->show_user_by_id($uid);// Get user info by UID
$nickname = $user_message['screen_name'];
$password = $_SESSION['token']['access_token'];
$credentials = array('email' => $uid, 'password' => $password);
if (Auth::attempt($credentials))
{
// Signin Success
return Redirect::intended();
} else {
$user = new User;
$user->email = $uid;
$user->password = $_SESSION['token']['access_token'];
$user->nickname = $nickname;
$user->save();
return View::make('authority.oauthSuccess');
}
} else {
return View::make('signup')
->withErrors(array('add' => 'Signup Fail'));;
}
}
/**
* View: Oauth Success
* @param string
* @return Response
*/
public function getOauthSuccess()
{
return View::make('authority.oauthSuccess');
}
// ...
File: app/views/authority/signup.blade.php
<?php
header("Content-type:text/html;charset=utf-8");
session_start();
include_once( app_path('api/config.php') );
include_once( app_path('api/saetv2.ex.class.php') );
$o = new SaeTOAuthV2( WB_AKEY , WB_SKEY );
$code_url = $o->getAuthorizeURL( WB_CALLBACK_URL );
?>
// ...
<a href="<?php echo $code_url ?>">
<button type="button">
<i class="fa fa-weibo fa-2x"></i>Connect Sina Weibo
</button>
</a>
// ...
File: app/views/authority/oauthSuccess.blade.php
<?php
header("Content-type:text/html;charset=utf-8");
include_once( app_path('api/config.php') );
include_once( app_path('api/saetv2.ex.class.php') );
$o = new SaeTOAuthV2( WB_AKEY , WB_SKEY );
$code_url = $o->getAuthorizeURL( WB_CALLBACK_URL );
?>
// ...
<p class="center">Authorization Successful</p>
<p>
<a href="<?php echo $code_url ?>">
<button type="button">
<i class="fa fa-weibo fa-2x"></i>
Login with your Weibo account
</button>
</a>
</p>
// ...
Result

Issues
ErrorException
Undefined variable: token
Only can signin with your own account, if you encounter this situation, try signin Sina Weibo Open Platform → Management Center → My Wesite → Click your website → Website Information → Test Account → Association Test Account.
Notice: curl_setopt(): CURLOPT_SSL_VERIFYHOST no longer accepts the value 1, value 2 will be used instead in saetv2.ex.class.php on line 357
Open saetv2.ex.class.php, comment out the code about line 356.
curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, 1);
Install Curl on Ubuntu Server
$ sudo apt-get install php5-curl
After install, don't forgot restart/reload web service.
Related Links Use Tencent QQ API in Laravel
I hope you like this tutorial. Share this tutorials on your favorite media to show your appreciation. Thanks!