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