PHP code example of shen2 / zen-oauth2
1. Go to this page and download the library: Download shen2/zen-oauth2 library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
shen2 / zen-oauth2 example snippets
$config = array(
'akey' => 'app key',
'skey' => 'secret key',
'scope' => 'email,friendships_groups_read',
);
$oauth = new ZenOAuth2\WeiboOAuth2($config['akey'], $config['skey']); //初始化oauth
$params = array(
'client_id' => $config['akey'],
'redirect_uri' => 'callback',//设置回调
'response_type' => 'code',
'state' => 'made by md5 avoid crsf',
'display' => null,
'scope' => $config['scope'],
'forcelogin' => 0, //是否使用已登陆微博账号
);
$authorizeUrl = $oauth->authorizeURL() . "?" . http_build_query($params);
header('Location : ' . $authorizeUrl);
//微博返回的code
$keys = array(
'code' => $_REQUEST['code'],
'redirect_uri'=> '{{redirec_uri}}',
);
//获取token
$token = $oauth->getAccessToken('code', $keys);
//根据上一步的acces_token实例化Client对象
$client = new ZenOAuth2\WeiboClient($token['access_token']);
//根据uid获取用户信息
$info = $client->get('users/show', array('uid'=>1739476392));
//删除一条微博
$data = $client->post('comments/destory', array('uid'=>1739476392, 'cid' => 'weiboid'));