一、配置相关信息
相关 api
设置回调页
二、参考代码
路由代码:
Route::get('test','home\ApiController@index');
Route::get('ok',function(){
echo "<a href='https://api.weibo.com/oauth2/authorize?client_id=AppKey&redirect_uri=回调url&response_type=code'>微博登录</a>";
});
ApiController 代码:
<?php namespace App\Http\Controllers\home;
use App\Http\Requests;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
class ApiController extends Controller {
public function index(Request $req){
//拼接需要的参数
$code=$req->only('code');
$api='https://api.weibo.com/oauth2/access_token';
$param=array();
$param['client_id']='AppKey';
$param['client_secret']='AppSecret';
$param['code']=$code['code'];
$param['redirect_uri']='http://weixin1.tunnel.2bdata.com/test';
//获取 access_token
$rs=$this->curl($api,$param);
$access_token=$rs['access_token'];
//获取用户 uid
$api='https://api.weibo.com/oauth2/get_token_info';
$param=array('access_token'=>$access_token);
$rs=$this->curl($api,$param);
$api='https://api.weibo.com/2/users/show.json';
$param=array(
'access_token'=>$access_token,
'uid'=>$rs['uid'],
);
$url=$api.'?'.http_build_query($param);
$url_res=file_get_contents($url);
print_r($url_res);
}
public function curl($api,$param){
$ch=curl_init($api);
curl_setopt($ch,CURLOPT_POST,1);
curl_setopt($ch,CURLOPT_POSTFIELDS,http_build_query($param));
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,0);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,0);
$rs=curl_exec($ch);
curl_close($ch);
return json_decode($rs,true);
}
}