新浪微博、腾讯微博、QQ、人人网、开心网、网易微博、豆瓣、百度、Google、微软、Instagram、Facebook、360、GitHub等平台的账号登录及api操作,使用oauth 2.0
官方提供的sdk都太过庞大,这是我自己简化的,提供简单的账号登录、获取个人信息、发布微博等功能,如果需要其他功能可以根据官方的api文档自行添加
2012-12-30:增加示例文件
2012-12-31:增加Facebook
2013-04-02:更新QQ、人人网、开心网、豆瓣、百度、Google、微软、Facebook
2013-05-09:增加GitHub
代码已提交到GIt @ OSC:http://git.oschina.net/piscdong/sns_php,这里将不再更新
<?php /** * PHP Library for weibo.com * * @author PiscDong (http://www.piscdong.com/) */ class sinaPHP { function __construct($client_id, $client_secret, $access_token=NULL){ $this->client_id=$client_id; $this->client_secret=$client_secret; $this->access_token=$access_token; } function login_url($callback_url){ $params=array( 'response_type'=>'code', 'client_id'=>$this->client_id, 'redirect_uri'=>$callback_url ); return 'https://api.weibo.com/oauth2/authorize?'.http_build_query($params); } function access_token($callback_url, $code){ $params=array( 'grant_type'=>'authorization_code', 'code'=>$code, 'client_id'=>$this->client_id, 'client_secret'=>$this->client_secret, 'redirect_uri'=>$callback_url ); $url='https://api.weibo.com/oauth2/access_token'; return $this->http($url, http_build_query($params), 'POST'); } /** function access_token_refresh($refresh_token){ } **/ function get_uid(){ $params=array(); $url='https://api.weibo.com/2/account/get_uid.json'; return $this->api($url, $params); } function show_user_by_id($uid){ $params=array( 'uid'=>$uid ); $url='https://api.weibo.com/2/users/show.json'; return $this->api($url, $params); } function statuses_count($ids){ $params=array( 'ids'=>$ids ); $url='https://api.weibo.com/2/statuses/count.json'; return $this->api($url, $params); } function get_comments_by_sid($id, $count=10, $page=1){ $params=array( 'id'=>$id, 'page'=>$page, 'count'=>$count ); $url='https://api.weibo.com/2/comments/show.json'; return $this->api($url, $params); } function repost_timeline($id, $count=10, $page=1){ $params=array( 'id'=>$id, 'page'=>$page, 'count'=>$count ); $url='https://api.weibo.com/2/statuses/repost_timeline.json'; return $this->api($url, $params); } function update($img_c, $pic=''){ $params=array( 'status'=>$img_c ); if($pic!='' && is_array($pic)){ $url='https://api.weibo.com/2/statuses/upload.json'; $params['pic']=$pic; }else{ $url='https://api.weibo.com/2/statuses/update.json'; } return $this->api($url, $params, 'POST'); } function user_timeline($uid, $count=10, $page=1){ $params=array( 'uid'=>$uid, 'page'=>$page, 'count'=>$count ); $url='https://api.weibo.com/2/statuses/user_timeline.json'; return $this->api($url, $params); } function querymid($id, $type=1, $is_batch=0){ $params=array( 'id'=>$id, 'type'=>$type, 'is_batch'=>$is_batch ); $url='https://api.weibo.com/2/statuses/querymid.json'; return $this->api($url, $params); } function api($url, $params, $method='GET'){ $params['access_token']=$this->access_token; if($method=='GET'){ $result=$this->http($url.'?'.http_build_query($params)); }else{ if(isset($params['pic'])){ uksort($params, 'strcmp'); $str_b=uniqid('------------------'); $str_m='--'.$str_b; $str_e=$str_m. '--'; $body=''; foreach($params as $k=>$v){ if($k=='pic'){ if(is_array($v)){ $img_c=$v[2]; $img_n=$v[1]; }elseif($v{0}=='@'){ $url=ltrim($v, '@'); $img_c=file_get_contents($url); $url_a=explode('?', basename($url)); $img_n=$url_a[0]; } $body.=$str_m."\r\n"; $body.='Content-Disposition: form-data; name="'.$k.'"; filename="'.$img_n.'"'."\r\n"; $body.="Content-Type: image/unknown\r\n\r\n"; $body.=$img_c."\r\n"; }else{ $body.=$str_m."\r\n"; $body.='Content-Disposition: form-data; name="'.$k."\"\r\n\r\n"; $body.=$v."\r\n"; } } $body.=$str_e; $headers[]="Content-Type: multipart/form-data; boundary=".$str_b; $result=$this->http($url, $body, 'POST', $headers); }else{ $result=$this->http($url, http_build_query($params), 'POST'); } } return $result; } function http($url, $postfields='', $method='GET', $headers=array()){ $ci=curl_init(); curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ci, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 30); curl_setopt($ci, CURLOPT_TIMEOUT, 30); if($method=='POST'){ curl_setopt($ci, CURLOPT_POST, TRUE); if($postfields!='')curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields); } $headers[]="User-Agent: sinaPHP(piscdong.com)"; curl_setopt($ci, CURLOPT_HTTPHEADER, $headers); curl_setopt($ci, CURLOPT_URL, $url); $response=curl_exec($ci); curl_close($ci); $json_r=array(); if($response!='')$json_r=json_decode($response, true); return $json_r; } }
2. [文件] 腾讯微博 ~ 5KB
<?php /** * PHP Library for t.qq.com * * @author PiscDong (http://www.piscdong.com/) */ class tqqPHP { function __construct($client_id, $client_secret, $access_token=NULL, $openid=NULL){ $this->client_id=$client_id; $this->client_secret=$client_secret; $this->access_token=$access_token; $this->openid=$openid; } function login_url($callback_url){ $params=array( 'response_type'=>'code', 'client_id'=>$this->client_id, 'redirect_uri'=>$callback_url ); return 'https://open.t.qq.com/cgi-bin/oauth2/authorize?'.http_build_query($params); } function access_token($callback_url, $code){ $params=array( 'grant_type'=>'authorization_code', 'code'=>$code, 'client_id'=>$this->client_id, 'client_secret'=>$this->client_secret, 'redirect_uri'=>$callback_url ); $url='https://open.t.qq.com/cgi-bin/oauth2/access_token?'.http_build_query($params); $result_str=$this->http($url); $json_r=array(); if($result_str!='')parse_str($result_str, $json_r); return $json_r; } function access_token_refresh($refresh_token){ $params=array( 'grant_type'=>'refresh_token', 'refresh_token'=>$refresh_token, 'client_id'=>$this->client_id ); $url='https://open.t.qq.com/cgi-bin/oauth2/access_token?'.http_build_query($params); $result_str=$this->http($url); $json_r=array(); if($result_str!='')parse_str($result_str, $json_r); return $json_r; } function me(){ $params=array(); $url='https://open.t.qq.com/api/user/info'; return $this->api($url, $params); } function getgetMyTweet($reqnum=10, $pageflag=0){ $params=array( 'pageflag'=>$pageflag, 'reqnum'=>$reqnum ); $url='https://open.t.qq.com/api/statuses/broadcast_timeline'; return $this->api($url, $params); } function getRecount($ids){ $params=array( 'ids'=>$ids, 'flag'=>2 ); $url='https://open.t.qq.com/api/t/re_count'; return $this->api($url, $params); } function getReplay($id, $flag=0, $f=0, $n=10){ $params=array( 'rootid'=>$id, 'pageflag'=>$f, 'reqnum'=>$n, 'flag'=>$flag ); $url='https://open.t.qq.com/api/t/re_list'; return $this->api($url, $params); } function postOne($img_c, $pic=''){ $params=array( 'content'=>$img_c ); if($pic!='' && is_array($pic)){ $url='https://open.t.qq.com/api/t/add_pic'; $params['pic']=$pic; }else{ $url='https://open.t.qq.com/api/t/add'; } return $this->api($url, $params, 'POST'); } function api($url, $params, $method='GET'){ $params['oauth_consumer_key']=$this->client_id; $params['access_token']=$this->access_token; $params['openid']=$this->openid; $params['clientip']=$this->getIP(); $params['oauth_version']='2.a'; $params['format']='json'; $params['scope']='all'; if($method=='GET'){ $result_str=$this->http($url.'?'.http_build_query($params)); }else{ if(isset($params['pic'])){ uksort($params, 'strcmp'); $str_b=uniqid('------------------'); $str_m='--'.$str_b; $str_e=$str_m. '--'; $body=''; foreach($params as $k=>$v){ if($k=='pic'){ if(is_array($v)){ $img_c=$v[2]; $img_n=$v[1]; }elseif($v{0}=='@'){ $url=ltrim($v, '@'); $img_c=file_get_contents($url); $url_a=explode('?', basename($url)); $img_n=$url_a[0]; } $body.=$str_m."\r\n"; $body.='Content-Disposition: form-data; name="'.$k.'"; filename="'.$img_n.'"'."\r\n"; $body.="Content-Type: image/unknown\r\n\r\n"; $body.=$img_c."\r\n"; }else{ $body.=$str_m."\r\n"; $body.='Content-Disposition: form-data; name="'.$k."\"\r\n\r\n"; $body.=$v."\r\n"; } } $body.=$str_e; $headers[]="Content-Type: multipart/form-data; boundary=".$str_b; $result_str=$this->http($url, $body, 'POST', $headers); }else{ $result_str=$this->http($url, http_build_query($params), 'POST'); } } $json_r=array(); if($result_str!='')$json_r=json_decode($result_str, true); return $json_r; } function getIP(){ if(isset($_ENV['HTTP_CLIENT_IP'])){ $ip=$_ENV['HTTP_CLIENT_IP']; }elseif(isset($_ENV['HTTP_X_FORWARDED_FOR'])){ $ip=$_ENV['HTTP_X_FORWARDED_FOR']; }elseif(isset($_ENV['REMOTE_ADDR'])){ $ip=$_ENV['REMOTE_ADDR']; }else{ $ip=$_SERVER['REMOTE_ADDR']; } if(strstr($ip, ':')){ $ipa=explode(':', $ip); foreach($ipa as $v){ if(strlen($v)>7)$ip=$v; } } if(strlen($ip)<7)$ip='0.0.0.0'; return $ip; } function http($url, $postfields='', $method='GET', $headers=array()){ $ci=curl_init(); curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ci, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 30); curl_setopt($ci, CURLOPT_TIMEOUT, 30); if($method=='POST'){ curl_setopt($ci, CURLOPT_POST, TRUE); if($postfields!='')curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields); } $headers[]="User-Agent: tqqPHP(piscdong.com)"; curl_setopt($ci, CURLOPT_HTTPHEADER, $headers); curl_setopt($ci, CURLOPT_URL, $url); $response=curl_exec($ci); curl_close($ci); return $response; } }
3. [文件] QQ ~ 3KB
<?php /** * PHP Library for qq.com * * @author PiscDong (http://www.piscdong.com/) */ class qqPHP { function __construct($appid, $appkey, $access_token=NULL){ $this->appid=$appid; $this->appkey=$appkey; $this->access_token=$access_token; } function login_url($callback_url, $scope=''){ $params=array( 'client_id'=>$this->appid, 'redirect_uri'=>$callback_url, 'response_type'=>'code', 'scope'=>$scope ); return 'https://graph.qq.com/oauth2.0/authorize?'.http_build_query($params); } function access_token($callback_url, $code){ $params=array( 'grant_type'=>'authorization_code', 'client_id'=>$this->appid, 'client_secret'=>$this->appkey, 'code'=>$code, 'state'=>'', 'redirect_uri'=>$callback_url ); $url='https://graph.qq.com/oauth2.0/token?'.http_build_query($params); $result_str=$this->http($url); $json_r=array(); if($result_str!='')parse_str($result_str, $json_r); return $json_r; } /** function access_token_refresh($refresh_token){ } **/ function get_openid(){ $params=array( 'access_token'=>$this->access_token ); $url='https://graph.qq.com/oauth2.0/me?'.http_build_query($params); $result_str=$this->http($url); $json_r=array(); if($result_str!=''){ preg_match('/callback\(\s+(.*?)\s+\)/i', $result_str, $result_a); $json_r=json_decode($result_a[1], true); } return $json_r; } function get_user_info($openid){ $params=array( 'openid'=>$openid ); $url='https://graph.qq.com/user/get_user_info'; return $this->api($url, $params); } function add_share($openid, $title, $url, $site, $fromurl, $images='', $summary=''){ $params=array( 'openid'=>$openid, 'title'=>$title, 'url'=>$url, 'site'=>$site, 'fromurl'=>$fromurl, 'images'=>$images, 'summary'=>$summary ); $url='https://graph.qq.com/share/add_share'; return $this->api($url, $params, 'POST'); } function api($url, $params, $method='GET'){ $params['access_token']=$this->access_token; $params['oauth_consumer_key']=$this->appid; $params['format']='json'; if($method=='GET'){ $result_str=$this->http($url.'?'.http_build_query($params)); }else{ $result_str=$this->http($url, http_build_query($params), 'POST'); } $result=array(); if($result_str!='')$result=json_decode($result_str, true); return $result; } function http($url, $postfields='', $method='GET', $headers=array()){ $ci=curl_init(); curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ci, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 30); curl_setopt($ci, CURLOPT_TIMEOUT, 30); if($method=='POST'){ curl_setopt($ci, CURLOPT_POST, TRUE); if($postfields!='')curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields); } $headers[]="User-Agent: qqPHP(piscdong.com)"; curl_setopt($ci, CURLOPT_HTTPHEADER, $headers); curl_setopt($ci, CURLOPT_URL, $url); $response=curl_exec($ci); curl_close($ci); return $response; } }
4. [文件] 人人网 ~ 3KB
<?php /** * PHP Library for renren.com * * @author PiscDong (http://www.piscdong.com/) */ class renrenPHP { function __construct($client_id, $client_secret, $access_token=NULL){ $this->client_id=$client_id; $this->client_secret=$client_secret; $this->access_token=$access_token; } function login_url($callback_url, $scope=''){ $params=array( 'response_type'=>'code', 'client_id'=>$this->client_id, 'redirect_uri'=>$callback_url, 'scope'=>$scope ); return 'https://graph.renren.com/oauth/authorize?'.http_build_query($params); } function access_token($callback_url, $code){ $params=array( 'grant_type'=>'authorization_code', 'code'=>$code, 'client_id'=>$this->client_id, 'client_secret'=>$this->client_secret, 'redirect_uri'=>$callback_url ); $url='https://graph.renren.com/oauth/token'; return $this->http($url, http_build_query($params), 'POST'); } function access_token_refresh($refresh_token){ $params=array( 'grant_type'=>'refresh_token', 'refresh_token'=>$refresh_token, 'client_id'=>$this->client_id, 'client_secret'=>$this->client_secret ); $url='https://graph.renren.com/oauth/token'; return $this->http($url, http_build_query($params), 'POST'); } function me(){ $params=array(); return $this->api('users.getInfo', $params, 'POST'); } function setStatus($status){ $params=array( 'status'=>$status ); return $this->api('status.set', $params, 'POST'); } function getStatus($uid, $count=10, $page=1){ $params=array( 'uid'=>$uid, 'page'=>$page, 'count'=>$count ); return $this->api('status.gets', $params, 'POST'); } function addBlog($title, $content){ $params=array( 'title'=>$title, 'content'=>$content ); return $this->api('blog.addBlog', $params, 'POST'); } function getBlog($id, $uid){ $params=array( 'id'=>$id, 'uid'=>$uid ); return $this->api('blog.get', $params, 'POST'); } function getComments($id, $uid, $count=10, $page=1){ $params=array( 'id'=>$id, 'uid'=>$uid, 'page'=>$page, 'count'=>$count ); return $this->api('blog.getComments', $params, 'POST'); } function api($method_name, $params, $method='GET'){ $params['method']=$method_name; $params['v']='1.0'; $params['access_token']=$this->access_token; $params['format']='json'; ksort($params); $sig_str=''; foreach($params as $k=>$v)$sig_str.=$k.'='.$v; $sig_str.=$this->client_secret; $sig=md5($sig_str); $params['sig']=$sig; $url='http://api.renren.com/restserver.do'; if($method=='GET'){ $result=$this->http($url.'?'.http_build_query($params)); }else{ $result=$this->http($url, http_build_query($params), 'POST'); } return $result; } function http($url, $postfields='', $method='GET', $headers=array()){ $ci=curl_init(); curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ci, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 30); curl_setopt($ci, CURLOPT_TIMEOUT, 30); if($method=='POST'){ curl_setopt($ci, CURLOPT_POST, TRUE); if($postfields!='')curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields); } $headers[]="User-Agent: renrenPHP(piscdong.com)"; curl_setopt($ci, CURLOPT_HTTPHEADER, $headers); curl_setopt($ci, CURLOPT_URL, $url); $response=curl_exec($ci); curl_close($ci); $json_r=array(); if($response!='')$json_r=json_decode($response, true); return $json_r; } }
5. [文件] 开心网 ~ 3KB
<?php /** * PHP Library for kaixin001.com * * @author PiscDong (http://www.piscdong.com/) */ class kaixinPHP { function __construct($client_id, $client_secret, $access_token=NULL){ $this->client_id=$client_id; $this->client_secret=$client_secret; $this->access_token=$access_token; } function login_url($callback_url, $scope=''){ $params=array( 'response_type'=>'code', 'client_id'=>$this->client_id, 'redirect_uri'=>$callback_url, 'scope'=>$scope ); return 'http://api.kaixin001.com/oauth2/authorize?'.http_build_query($params); } function access_token($callback_url, $code){ $params=array( 'grant_type'=>'authorization_code', 'code'=>$code, 'client_id'=>$this->client_id, 'client_secret'=>$this->client_secret, 'redirect_uri'=>$callback_url ); $url='https://api.kaixin001.com/oauth2/access_token'; return $this->http($url, http_build_query($params), 'POST'); } function access_token_refresh($refresh_token){ $params=array( 'grant_type'=>'refresh_token', 'refresh_token'=>$refresh_token, 'client_id'=>$this->client_id, 'client_secret'=>$this->client_secret ); $url='https://api.kaixin001.com/oauth2/access_token'; return $this->http($url, http_build_query($params), 'POST'); } function me(){ $params=array(); $url='https://api.kaixin001.com/users/me.json'; return $this->api($url, $params); } function records_add($content, $picurl=''){ $params=array( 'content'=>$content ); if($picurl!='')$params['picurl']=$picurl; $url='https://api.kaixin001.com/records/add.json'; return $this->api($url, $params, 'POST'); } function records_me($num=10, $start=0){ $params=array( 'start'=>$start, 'num'=>$num ); $url='https://api.kaixin001.com/records/me.json'; return $this->api($url, $params); } function comment_list($id, $uid, $num=10, $start=0){ $params=array( 'objtype'=>'records', 'objid'=>$id, 'ouid'=>$uid, 'start'=>$start, 'num'=>$num ); $url='https://api.kaixin001.com/comment/list.json'; return $this->api($url, $params); } function forward_list($id, $uid, $num=10, $start=0){ $params=array( 'objtype'=>'records', 'objid'=>$id, 'ouid'=>$uid, 'start'=>$start, 'num'=>$num ); $url='https://api.kaixin001.com/forward/list.json'; return $this->api($url, $params); } function like_show($id, $uid, $num=10, $start=0){ $params=array( 'objtype'=>'records', 'objid'=>$id, 'ouid'=>$uid, 'start'=>$start, 'num'=>$num ); $url='https://api.kaixin001.com/like/show.json'; return $this->api($url, $params); } function api($url, $params, $method='GET'){ $params['access_token']=$this->access_token; if($method=='GET'){ $result=$this->http($url.'?'.http_build_query($params)); }else{ $result=$this->http($url, http_build_query($params), 'POST'); } return $result; } function http($url, $postfields='', $method='GET', $headers=array()){ $ci=curl_init(); curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ci, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 30); curl_setopt($ci, CURLOPT_TIMEOUT, 30); if($method=='POST'){ curl_setopt($ci, CURLOPT_POST, TRUE); if($postfields!='')curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields); } $headers[]="User-Agent: kaixinPHP(piscdong.com)"; curl_setopt($ci, CURLOPT_HTTPHEADER, $headers); curl_setopt($ci, CURLOPT_URL, $url); $response=curl_exec($ci); curl_close($ci); $json_r=array(); if($response!='')$json_r=json_decode($response, true); return $json_r; } }
6. [文件] 网易微博 ~ 3KB
<?php /** * PHP Library for t.163.com * * @author PiscDong (http://www.piscdong.com/) */ class t163PHP { function __construct($client_id, $client_secret, $access_token=NULL){ $this->client_id=$client_id; $this->client_secret=$client_secret; $this->access_token=$access_token; } function login_url($callback_url){ $params=array( 'response_type'=>'code', 'client_id'=>$this->client_id, 'redirect_uri'=>$callback_url ); return 'https://api.t.163.com/oauth2/authorize?'.http_build_query($params); } function access_token($callback_url, $code){ $params=array( 'grant_type'=>'authorization_code', 'code'=>$code, 'client_id'=>$this->client_id, 'client_secret'=>$this->client_secret, 'redirect_uri'=>$callback_url ); $url='https://api.t.163.com/oauth2/access_token'; return $this->http($url, http_build_query($params), 'POST'); } function access_token_refresh($refresh_token){ $params=array( 'grant_type'=>'refresh_token', 'refresh_token'=>$refresh_token, 'client_id'=>$this->client_id, 'client_secret'=>$this->client_secret ); $url='https://api.t.163.com/oauth2/access_token'; return $this->http($url, http_build_query($params), 'POST'); } function me(){ $params=array(); $url='https://api.t.163.com/users/show.json'; return $this->api($url, $params); } function user_timeline($id, $count=10){ $params=array( 'user_id'=>$id, 'count'=>$count ); $url='https://api.t.163.com/statuses/user_timeline.json'; return $this->api($url, $params); } function update($status){ $params=array( 'status'=>$status ); $url='https://api.t.163.com/statuses/update.json'; return $this->api($url, $params, 'POST'); } function api($url, $params, $method='GET'){ $params['access_token']=$this->access_token; if($method=='GET'){ $result=$this->http($url.'?'.http_build_query($params)); }else{ $result=$this->http($url, http_build_query($params), 'POST'); } return $result; } function http($url, $postfields='', $method='GET', $headers=array()){ $ci=curl_init(); curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ci, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 30); curl_setopt($ci, CURLOPT_TIMEOUT, 30); if($method=='POST'){ curl_setopt($ci, CURLOPT_POST, TRUE); if($postfields!='')curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields); } $headers[]="User-Agent: t163PHP(piscdong.com)"; curl_setopt($ci, CURLOPT_HTTPHEADER, $headers); curl_setopt($ci, CURLOPT_URL, $url); $response=curl_exec($ci); curl_close($ci); $json_r=array(); if($response!='')$json_r=json_decode($response, true); return $json_r; } }
7. [文件] 豆瓣 ~ 3KB
<?php /** * PHP Library for douban.com * * @author PiscDong (http://www.piscdong.com/) */ class doubanPHP { function __construct($client_id, $client_secret, $access_token=NULL){ $this->client_id=$client_id; $this->client_secret=$client_secret; $this->access_token=$access_token; } function login_url($callback_url, $scope=''){ $params=array( 'response_type'=>'code', 'client_id'=>$this->client_id, 'redirect_uri'=>$callback_url, 'scope'=>$scope, 'state'=>md5(time()) ); return 'https://www.douban.com/service/auth2/auth?'.http_build_query($params); } function access_token($callback_url, $code){ $params=array( 'grant_type'=>'authorization_code', 'code'=>$code, 'client_id'=>$this->client_id, 'client_secret'=>$this->client_secret, 'redirect_uri'=>$callback_url ); $url='https://www.douban.com/service/auth2/token'; return $this->http($url, http_build_query($params), 'POST'); } function access_token_refresh($callback_url, $refresh_token){ $params=array( 'grant_type'=>'refresh_token', 'refresh_token'=>$refresh_token, 'client_id'=>$this->client_id, 'client_secret'=>$this->client_secret, 'redirect_uri'=>$callback_url ); $url='https://www.douban.com/service/auth2/token'; return $this->http($url, http_build_query($params), 'POST'); } function me(){ $params=array(); $url='https://api.douban.com/v2/user/~me'; return $this->api($url, $params); } function share($text, $title, $url, $description='', $pic=''){ $params=array( 'text'=>$text, 'rec_title'=>$title, 'rec_url'=>$url, 'rec_desc'=>$description, 'rec_image'=>$pic ); $url='https://api.douban.com/shuo/v2/statuses/'; return $this->api($url, $params, 'POST'); } function api($url, $params, $method='GET'){ $headers[]="Authorization: Bearer ".$this->access_token; if($method=='GET'){ $result=$this->http($url.'?'.http_build_query($params), '', 'GET', $headers); }else{ $result=$this->http($url, http_build_query($params), 'POST', $headers); } return $result; } function http($url, $postfields='', $method='GET', $headers=array()){ $ci=curl_init(); curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ci, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 30); curl_setopt($ci, CURLOPT_TIMEOUT, 30); if($method=='POST'){ curl_setopt($ci, CURLOPT_POST, TRUE); if($postfields!='')curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields); } $headers[]="User-Agent: doubanPHP(piscdong.com)"; curl_setopt($ci, CURLOPT_HTTPHEADER, $headers); curl_setopt($ci, CURLOPT_URL, $url); $response=curl_exec($ci); curl_close($ci); $json_r=array(); if($response!='')$json_r=json_decode($response, true); return $json_r; } }
8. [文件] 百度 ~ 2KB
<?php /** * PHP Library for baidu.com * * @author PiscDong (http://www.piscdong.com/) */ class baiduPHP { function __construct($client_id, $client_secret, $access_token=NULL){ $this->client_id=$client_id; $this->client_secret=$client_secret; $this->access_token=$access_token; } function login_url($callback_url, $scope=''){ $params=array( 'response_type'=>'code', 'client_id'=>$this->client_id, 'redirect_uri'=>$callback_url, 'scope'=>$scope, 'state'=>md5(time()), 'display'=>'page' ); return 'https://openapi.baidu.com/oauth/2.0/authorize?'.http_build_query($params); } function access_token($callback_url, $code){ $params=array( 'grant_type'=>'authorization_code', 'code'=>$code, 'client_id'=>$this->client_id, 'client_secret'=>$this->client_secret, 'redirect_uri'=>$callback_url ); $url='https://openapi.baidu.com/oauth/2.0/token'; return $this->http($url, http_build_query($params), 'POST'); } function access_token_refresh($refresh_token){ $params=array( 'grant_type'=>'refresh_token', 'refresh_token'=>$refresh_token, 'client_id'=>$this->client_id, 'client_secret'=>$this->client_secret ); $url='https://openapi.baidu.com/oauth/2.0/token'; return $this->http($url, http_build_query($params), 'POST'); } function user(){ $params=array(); $url='https://openapi.baidu.com/rest/2.0/passport/users/getLoggedInUser'; return $this->api($url, $params); } function api($url, $params, $method='GET'){ $params['access_token']=$this->access_token; if($method=='GET'){ $result=$this->http($url.'?'.http_build_query($params)); }else{ $result=$this->http($url, http_build_query($params), 'POST'); } return $result; } function http($url, $postfields='', $method='GET', $headers=array()){ $ci=curl_init(); curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ci, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 30); curl_setopt($ci, CURLOPT_TIMEOUT, 30); if($method=='POST'){ curl_setopt($ci, CURLOPT_POST, TRUE); if($postfields!='')curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields); } $headers[]="User-Agent: baiduPHP(piscdong.com)"; curl_setopt($ci, CURLOPT_HTTPHEADER, $headers); curl_setopt($ci, CURLOPT_URL, $url); $response=curl_exec($ci); curl_close($ci); $json_r=array(); if($response!='')$json_r=json_decode($response, true); return $json_r; } }
9. [文件] Google ~ 2KB
<?php /** * PHP Library for google.com * * @author PiscDong (http://www.piscdong.com/) */ class googlePHP { function __construct($client_id, $client_secret, $access_token=NULL){ $this->client_id=$client_id; $this->client_secret=$client_secret; $this->access_token=$access_token; } function login_url($callback_url, $scope=''){ $params=array( 'response_type'=>'code', 'client_id'=>$this->client_id, 'redirect_uri'=>$callback_url, 'scope'=>$scope, 'state'=>'profile', 'access_type'=>'offline' ); return 'https://accounts.google.com/o/oauth2/auth?'.http_build_query($params); } function access_token($callback_url, $code){ $params=array( 'grant_type'=>'authorization_code', 'code'=>$code, 'client_id'=>$this->client_id, 'client_secret'=>$this->client_secret, 'redirect_uri'=>$callback_url ); $url='https://accounts.google.com/o/oauth2/token'; $result=$this->http($url, http_build_query($params), 'POST'); return $result; } function access_token_refresh($refresh_token){ $params=array( 'grant_type'=>'refresh_token', 'refresh_token'=>$refresh_token, 'client_id'=>$this->client_id, 'client_secret'=>$this->client_secret ); $url='https://accounts.google.com/o/oauth2/token'; $result=$this->http($url, http_build_query($params), 'POST'); return $result; } function me(){ $params=array(); $url='https://www.googleapis.com/oauth2/v1/userinfo'; return $this->api($url, $params); } function api($url, $params, $method='GET'){ $headers[]="Authorization: Bearer ".$this->access_token; if($method=='GET'){ $result=$this->http($url.'?'.http_build_query($params), '', 'GET', $headers); }else{ $result=$this->http($url, http_build_query($params), 'POST', $headers); } return $result; } function http($url, $postfields='', $method='GET', $headers=array()){ $ci=curl_init(); curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ci, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 30); curl_setopt($ci, CURLOPT_TIMEOUT, 30); if($method=='POST'){ curl_setopt($ci, CURLOPT_POST, TRUE); if($postfields!='')curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields); } $headers[]="User-Agent: google(piscdong.com)"; curl_setopt($ci, CURLOPT_HTTPHEADER, $headers); curl_setopt($ci, CURLOPT_URL, $url); $response=curl_exec($ci); curl_close($ci); $json_r=array(); if($response!='')$json_r=json_decode($response, true); return $json_r; } }
10. [文件] 微软 ~ 2KB
<?php /** * PHP Library for live.com * * @author PiscDong (http://www.piscdong.com/) */ class livePHP { function __construct($client_id, $client_secret, $access_token=NULL){ $this->client_id=$client_id; $this->client_secret=$client_secret; $this->access_token=$access_token; } function login_url($callback_url, $scope=''){ $params=array( 'response_type'=>'code', 'client_id'=>$this->client_id, 'redirect_uri'=>$callback_url, 'scope'=>$scope ); return 'https://login.live.com/oauth20_authorize.srf?'.http_build_query($params); } function access_token($callback_url, $code){ $params=array( 'grant_type'=>'authorization_code', 'code'=>$code, 'client_id'=>$this->client_id, 'client_secret'=>$this->client_secret, 'redirect_uri'=>$callback_url ); $url='https://login.live.com/oauth20_token.srf'; return $this->http($url, http_build_query($params), 'POST'); } function access_token_refresh($refresh_token){ $params=array( 'grant_type'=>'refresh_token', 'refresh_token'=>$refresh_token, 'client_secret'=>$this->client_secret, 'client_id'=>$this->client_id ); $url='https://login.live.com/oauth20_token.srf'; return $this->http($url, http_build_query($params), 'POST'); } function me(){ $params=array(); $url='https://apis.live.net/v5.0/me'; return $this->api($url, $params); } function api($url, $params, $method='GET'){ $params['access_token']=$this->access_token; if($method=='GET'){ $result=$this->http($url.'?'.http_build_query($params)); }else{ $result=$this->http($url, http_build_query($params), 'POST'); } return $result; } function http($url, $postfields='', $method='GET', $headers=array()){ $ci=curl_init(); curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ci, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 30); curl_setopt($ci, CURLOPT_TIMEOUT, 30); if($method=='POST'){ curl_setopt($ci, CURLOPT_POST, TRUE); if($postfields!='')curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields); } $headers[]="User-Agent: livePHP(piscdong.com)"; curl_setopt($ci, CURLOPT_HTTPHEADER, $headers); curl_setopt($ci, CURLOPT_URL, $url); $response=curl_exec($ci); curl_close($ci); $json_r=array(); if($response!='')$json_r=json_decode($response, true); return $json_r; } }
11. [文件] Instagram ~ 2KB
<?php /** * PHP Library for instagram.com * * @author PiscDong (http://www.piscdong.com/) */ class instagramPHP { function __construct($client_id, $client_secret, $access_token=NULL){ $this->client_id=$client_id; $this->client_secret=$client_secret; $this->access_token=$access_token; } function login_url($callback_url){ $params=array( 'response_type'=>'code', 'client_id'=>$this->client_id, 'redirect_uri'=>$callback_url ); return 'https://api.instagram.com/oauth/authorize/?'.http_build_query($params); } function access_token($callback_url, $code){ $params=array( 'grant_type'=>'authorization_code', 'code'=>$code, 'client_id'=>$this->client_id, 'client_secret'=>$this->client_secret, 'redirect_uri'=>$callback_url ); $url='https://api.instagram.com/oauth/access_token'; return $this->http($url, http_build_query($params), 'POST'); } /** function access_token_refresh($refresh_token){ } **/ function user($id){ $params=array(); $url='https://api.instagram.com/v1/users/'.$id.'/'; return $this->api($url, $params); } function user_media($id, $count=10, $max_id=''){ $params=array( 'count'=>$count ); if($max_id!='')$params['max_id']=$max_id; $url='https://api.instagram.com/v1/users/'.$id.'/media/recent/'; return $this->api($url, $params); } function api($url, $params, $method='GET'){ $params['access_token']=$this->access_token; if($method=='GET'){ $result=$this->http($url.'?'.http_build_query($params)); }else{ $result=$this->http($url, http_build_query($params), 'POST'); } return $result; } function http($url, $postfields='', $method='GET', $headers=array()){ $ci=curl_init(); curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ci, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 30); curl_setopt($ci, CURLOPT_TIMEOUT, 30); if($method=='POST'){ curl_setopt($ci, CURLOPT_POST, TRUE); if($postfields!='')curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields); } $headers[]="User-Agent: instagramPHP(piscdong.com)"; curl_setopt($ci, CURLOPT_HTTPHEADER, $headers); curl_setopt($ci, CURLOPT_URL, $url); $response=curl_exec($ci); curl_close($ci); $json_r=array(); if($response!='')$json_r=json_decode($response, true); return $json_r; } }
12. [文件] Facebook ~ 2KB
<?php /** * PHP Library for facebook.com * * @author PiscDong (http://www.piscdong.com/) */ class facebookPHP { function __construct($client_id, $client_secret, $access_token=NULL){ $this->client_id=$client_id; $this->client_secret=$client_secret; $this->access_token=$access_token; } function login_url($callback_url, $scope=''){ $params=array( 'response_type'=>'code', 'client_id'=>$this->client_id, 'redirect_uri'=>$callback_url, 'scope'=>$scope ); return 'https://graph.facebook.com/oauth/authorize?'.http_build_query($params); } function access_token($callback_url, $code){ $params=array( 'grant_type'=>'authorization_code', 'code'=>$code, 'client_id'=>$this->client_id, 'client_secret'=>$this->client_secret, 'redirect_uri'=>$callback_url ); $url='https://graph.facebook.com/oauth/access_token'; return $this->http($url, http_build_query($params), 'POST'); } /** function access_token_refresh($refresh_token){ } **/ function me(){ $params=array(); $url='https://graph.facebook.com/me'; return $this->api($url, $params); } function my_feed($count=10, $page=1){ $params=array( 'page'=>$page, 'count'=>$count ); $url='https://graph.facebook.com/me/feed'; return $this->api($url, $params); } function update($content){ $params=array( 'message'=>$content ); $url='https://graph.facebook.com/me/feed/'; return $this->api($url, $params, 'POST'); } function api($url, $params, $method='GET'){ $params['access_token']=$this->access_token; if($method=='GET'){ $result=$this->http($url.'?'.http_build_query($params)); }else{ $result=$this->http($url, http_build_query($params), 'POST'); } return $result; } function http($url, $postfields='', $method='GET', $headers=array()){ $ci=curl_init(); curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ci, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 30); curl_setopt($ci, CURLOPT_TIMEOUT, 30); if($method=='POST'){ curl_setopt($ci, CURLOPT_POST, TRUE); if($postfields!='')curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields); } $headers[]="User-Agent: facebookPHP(piscdong.com)"; curl_setopt($ci, CURLOPT_HTTPHEADER, $headers); curl_setopt($ci, CURLOPT_URL, $url); $response=curl_exec($ci); curl_close($ci); $json_r=array(); if($response!='')$json_r=json_decode($response, true); return $json_r; } }
13. [文件] 360 ~ 2KB
<?php /** * PHP Library for 360.cn * * @author PiscDong (http://www.piscdong.com/) */ class o360PHP { function __construct($client_id, $client_secret, $access_token=NULL){ $this->client_id=$client_id; $this->client_secret=$client_secret; $this->access_token=$access_token; } function login_url($callback_url){ $params=array( 'response_type'=>'code', 'client_id'=>$this->client_id, 'redirect_uri'=>$callback_url ); return 'https://openapi.360.cn/oauth2/authorize?'.http_build_query($params); } function access_token($callback_url, $code){ $params=array( 'grant_type'=>'authorization_code', 'code'=>$code, 'client_id'=>$this->client_id, 'client_secret'=>$this->client_secret, 'redirect_uri'=>$callback_url ); $url='https://openapi.360.cn/oauth2/access_token'; return $this->http($url, http_build_query($params), 'POST'); } function access_token_refresh($refresh_token){ $params=array( 'grant_type'=>'refresh_token', 'refresh_token'=>$refresh_token, 'client_id'=>$this->client_id, 'client_secret'=>$this->client_secret ); $url='https://openapi.360.cn/oauth2/access_token'; return $this->http($url, http_build_query($params), 'POST'); } function me(){ $params=array(); $url='https://openapi.360.cn/user/me.json'; return $this->api($url, $params); } function api($url, $params, $method='GET'){ $params['access_token']=$this->access_token; if($method=='GET'){ $result=$this->http($url.'?'.http_build_query($params)); }else{ $result=$this->http($url, http_build_query($params), 'POST'); } return $result; } function http($url, $postfields='', $method='GET', $headers=array()){ $ci=curl_init(); curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ci, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 30); curl_setopt($ci, CURLOPT_TIMEOUT, 30); if($method=='POST'){ curl_setopt($ci, CURLOPT_POST, TRUE); if($postfields!='')curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields); } $headers[]="User-Agent: o360PHP(piscdong.com)"; curl_setopt($ci, CURLOPT_HTTPHEADER, $headers); curl_setopt($ci, CURLOPT_URL, $url); $response=curl_exec($ci); curl_close($ci); $json_r=array(); if($response!='')$json_r=json_decode($response, true); return $json_r; } }
14. [文件] github.php ~ 2KB
<?php /** * PHP Library for github.com * * @author PiscDong (http://www.piscdong.com/) */ class githubPHP { function __construct($client_id, $client_secret, $access_token=NULL){ $this->client_id=$client_id; $this->client_secret=$client_secret; $this->access_token=$access_token; } function login_url($callback_url, $scope=''){ $params=array( 'client_id'=>$this->client_id, 'redirect_uri'=>$callback_url, 'scope'=>$scope ); return 'https://github.com/login/oauth/authorize?'.http_build_query($params); } function access_token($callback_url, $code){ $params=array( 'code'=>$code, 'client_id'=>$this->client_id, 'client_secret'=>$this->client_secret, 'redirect_uri'=>$callback_url ); $url='https://github.com/login/oauth/access_token'; $result_str=$this->http($url, http_build_query($params), 'POST'); $json_r=array(); if($result_str!='')parse_str($result_str, $json_r); return $json_r; } /** function access_token_refresh($refresh_token){ } **/ function me(){ $params=array(); $url='https://api.github.com/user'; return $this->api($url, $params); } function api($url, $params, $method='GET'){ $params['access_token']=$this->access_token; if($method=='GET'){ $result_str=$this->http($url.'?'.http_build_query($params)); }else{ $result_str=$this->http($url, http_build_query($params), 'POST'); } $result=array(); if($result_str!='')$result=json_decode($result_str, true); return $result; } function http($url, $postfields='', $method='GET', $headers=array()){ $ci=curl_init(); curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ci, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 30); curl_setopt($ci, CURLOPT_TIMEOUT, 30); if($method=='POST'){ curl_setopt($ci, CURLOPT_POST, TRUE); if($postfields!='')curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields); } $headers[]="User-Agent: githubPHP(piscdong.com)"; curl_setopt($ci, CURLOPT_HTTPHEADER, $headers); curl_setopt($ci, CURLOPT_URL, $url); $response=curl_exec($ci); curl_close($ci); return $response; } }
15. [代码]示例文件,以新浪微博为例,其他平台方法类似
/** * 示例文件,以新浪微博为例,其他平台方法类似 */ //生成登录链接 require_once('sina.php'); $app_key=''; //新浪微博应用App Key $app_secret=''; //新浪微博应用App Secret $callback_url='http://yoururl/sina_callback.php'; //回调网址,请根据自己的实际情况修改 $sina=new sinaPHP($app_key, $app_secret); $login_url=$sina->login_url($callback_url); //生成登录链接,部分平台需要权限,格式请参考各平台api文档 echo '<a href="'.$login_url.'">点击进入授权页面</a>'; //授权回调页面,即生成登录链接时的$callback_url require_once('sina.php'); $app_key=''; //新浪微博应用App Key $app_secret=''; //新浪微博应用App Secret $callback_url='http://yoururl/sina_callback.php'; //回调网址,必须和生成登录链接时相同 if(isset($_GET['code']) && $_GET['code']!=''){ $sina=new sinaPHP($app_key, $app_secret); $result=$sina->access_token($callback_url, $_GET['code']); //获取access token /** * $result['access_token'],用户access token * $result['expires_in'],access token的有效期,单位:秒 * 部分平台会有$result['refresh_token'],refresh token,access token到期后使用refresh token生成新的access token * 腾讯微博还需要保存$_GET['openid'] */ } //用户登录授权后操作api require_once('sina.php'); $app_key=''; //新浪微博应用App Key $app_secret=''; //新浪微博应用App Secret $access_token=''; //授权回调页面生成的用户access token $sina=new sinaPHP($app_key, $app_secret, $access_token); //腾讯微博还需要openid,授权回调页面保存的$_GET['openid'] $sina_uid=$sina->get_uid(); //登录用户uid //其他功能请参考sina.php自行使用,或者根据api文档自行添加 //其他平台的使用方法和新浪微博类似,各种api的返回数据格式各有不同,请自行参考各平台的api文档 //支持refresh token的平台在access token到期后请使用access_token_refresh()生成新的access token
来源:http://www.oschina.net/code/snippet_930167_16877#28044