Use Tencent QQ API in Laravel

Signin Tencent QQ Connect, get App ID and App Key, reference PHP SDK. The main principle is after authority on Tencent QQ account, store openid, 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_19_1024_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/qq:

├── app
│   ├── api
│   │   ├── qq
│   │   │   ├── class
│   │   │   │   ├── ErrorCase.class.php
│   │   │   │   ├── Oauth.class.php
│   │   │   │   ├── QC.class.php
│   │   │   │   ├── Recorder.class.php
│   │   │   │   └── URL.class.php
│   │   │   ├── comm
│   │   │   │   ├── config.php
│   │   │   │   ├── inc.php
│   │   │   │   └── utils.php
│   │   │   ├── qqConnectAPI.php
│   │   │   └── storage
│   │   ├── weibo
│   │   ├── ...
│   ├── commands
│   ├── config
│   ├── controllers
│   ├── database
│   ├── lang
│   ├── ...
├── bootstrap
├── public
├── readme
├── vendor
├── ...
...

File: app/api/qq/comm/inc.php

<?php die('forbidden'); ?>
{"appid":"xxxxxxxxx","appkey":"xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx","callback":"yourdomain/auth/oauth-qq","scope":"get_user_info,add_share,list_album,add_album,upload_pic,add_topic,add_one_blog,add_weibo,check_page_fans,add_t,add_pic_t,del_t,get_repost_list,get_info,get_other_info,get_fanslist,get_idolist,add_idol,del_idol,get_tenpay_addr","errorReport":true,"storageType":"file","host":"localhost","user":"root","password":"root","database":"test"}

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 QQ Signup
        Route::get('oauth-qq', array('as' => 'oauth-qq', 'uses' => $Authority.'getOauthQQ'));
        // ...
    });
});
// ...

File: AuthorityController.php

// ...

/**
 * Action: Oauth QQ
 * @return Response
 */
public function getOauthQQ()
{
    include_once( app_path('api/qq/qqConnectAPI.php' ));
    $qc = new QC();
    $callback     = $qc->qq_callback();
    $openid       = $qc->get_openid();

    $qc = new QC($callback,$openid);
    $access_token = $qc->get_access_token();
    $arr          = $qc->get_user_info();
    $nickname     = $arr["nickname"];
    $credentials  = array('email' => $openid, 'password' => $access_token);

    if (Auth::attempt($credentials))
    {
        // Signin Success
        return Redirect::intended();
    } else {
        $user           = new User;
        $user->email    = $openid;
        $user->password = $access_token;
        $user->nickname = $nickname;
        $user->save();
        return View::make('authority.oauthQQ');
    }

}

// ...

File: app/views/authority/signup.blade.php

// ...
<a href="{{ home('route') }}/oauth-qq.php">
<button type="button">
<i class="fa fa-qq fa-2x"></i>Connect Tencent QQ
</button>
</a>
// ...

File: app/views/authority/oauthQQ.blade.php

// ...
<p class="center">Authorization Successful</p>
<p>
    <a href="{{ route('home') }}/oauth-qq.php">
        <button type="button">
            <i class="fa fa-qq fa-2x"></i>
            Login with your QQ account
        </button>
    </a>
</p>
// ...

File: public/ouath-qq.php

<?php

require_once("../app/api/qq/qqConnectAPI.php");
$qc = new QC();
$qc->qq_login();

Result

Use Tencent QQ API in Laravel

Related Links Use Sina Weibo OAuth 2.0 Authorize in Laravel

I hope you like this tutorial. Share this tutorials on your favorite media to show your appreciation. Thanks!

0.00 avg. rating (0% score) - 0 votes