Rumah >pembangunan bahagian belakang >tutorial php >新浪,腾讯,淘宝登陆_PHP教程
可以直接拿去用了
<?<span php </span><span session_start</span><span (); </span><span class</span><span openlogin{ </span><span public</span> <span $_URL</span> = ""<span ; </span><span public</span> <span $config</span> = <span array</span><span (); </span><span public</span> <span function</span><span __construct(){ </span><span $this</span>-><span openlogin(); } </span><span function</span><span openlogin(){ } </span><span /*</span><span 获取登陆页面URL</span><span */</span> <span public</span> <span function</span><span login_url(){ </span><span if</span>(<span empty</span>(<span $this</span>-><span config)){ </span><span return</span> <span false</span><span ; } </span><span $config</span> = <span $this</span>-><span config; </span><span $login_url</span> = <span $config</span>['login_url'<span ]; </span><span $_SESSION</span>['state'] = <span $state</span> = <span md5</span>(<span uniqid</span>(<span rand</span>(), <span TRUE</span><span )); </span><span $array</span> = <span array</span><span ( </span>"response_type"=>"code", "state" => <span $state</span>, "client_id"=><span $config</span>['appkey'], "redirect_uri"=><span urlencode</span>( <span $config</span>['redirect_uri'<span ] ) ); </span><span $this</span>->set(<span $array</span><span ); </span><span $url</span> = <span $this</span>->combineURL(<span $login_url</span> , <span $this</span>-><span _param); </span><span if</span>(<span $url</span><span ){ @</span><span header</span>("Location:".<span $url</span><span ); }</span><span else</span><span { </span><span return</span> <span false</span><span ; } } </span><span /*</span><span 获取access_token</span><span */</span> <span public</span> <span function</span><span get_access_token(){ </span><span if</span>(<span empty</span>(<span $this</span>-><span config)){ </span><span return</span> <span false</span><span ; } </span><span $config</span> = <span $this</span>-><span config; </span><span if</span>(! <span $config</span>['code'] = <span $_REQUEST</span>['code'<span ] ){ </span><span return</span> <span false</span><span ; } </span><span $url</span> = <span $config</span>['authorization_url'<span ]; </span><span $state</span> = <span $_SESSION</span>['state'<span ]; </span><span $array</span> = <span array</span><span ( </span>"grant_type"=>"authorization_code", "client_id" => <span $config</span>['appkey'], "client_secret"=><span $config</span>['appsecret'], "code"=><span $config</span>['code'], "redirect_uri"=><span urlencode</span>( <span $config</span>['redirect_uri'] ), "state"=><span $state</span><span ); </span><span $this</span>->set(<span $array</span><span ); </span><span return</span> <span $this</span>->post_contents(<span $url</span><span ); } </span><span /*</span><span set $this->_param 数组</span><span */</span> <span public</span> <span function</span> set(<span $array</span><span ) { </span><span if</span>(<span empty</span>(<span $array</span><span )){ </span><span return</span> <span false</span><span ; } </span><span $this</span>->_param = <span array</span><span (); </span><span foreach</span>(<span $array</span> <span as</span> <span $name</span>=><span $value</span><span ){ </span><span $this</span>->_param[<span $name</span>] = <span $value</span><span ; } } </span><span /*</span><span * * post_contents * 服务器通过post请求获得内容 * @param string $url 请求的url,拼接后的 * @return string 请求返回的内容 </span><span */</span> <span public</span> <span function</span> post_contents(<span $url</span><span ){ </span><span if</span>(<span empty</span>(<span $url</span><span )){ </span><span return</span> <span false</span><span ; } </span><span $param</span> = <span $this</span>->combineURL("" , <span $this</span>-><span _param); </span><span $ch</span> =<span curl_init(); </span><span //</span><span 设置URL和相应的选项</span> curl_setopt(<span $ch</span>, CURLOPT_URL, <span $url</span><span ); curl_setopt(</span><span $ch</span>, CURLOPT_FAILONERROR, <span false</span><span ); curl_setopt(</span><span $ch</span>, CURLOPT_RETURNTRANSFER, <span true</span><span ); curl_setopt (</span><span $ch</span>, CURLOPT_SSL_VERIFYPEER, 0<span ); curl_setopt (</span><span $ch</span>, CURLOPT_SSL_VERIFYHOST, 0<span ); curl_setopt(</span><span $ch</span>, CURLOPT_POST, 1<span ); curl_setopt(</span><span $ch</span>, CURLOPT_POSTFIELDS, <span $param</span><span ); </span><span //</span><span 抓取URL并把它传递给浏览器</span> <span $reponse</span> = curl_exec(<span $ch</span><span ); curl_close(</span><span $ch</span><span ); </span><span return</span> <span $reponse</span><span ; } </span><span /*</span><span * * get_contents * 服务器通过get请求获得内容 * @param string $url 请求的url,拼接后的 * @return string 请求返回的内容 </span><span */</span> <span public</span> <span function</span> get_contents(<span $url</span><span ){ </span><span $ch</span> =<span curl_init(); curl_setopt(</span><span $ch</span>, CURLOPT_SSL_VERIFYPEER, <span FALSE</span><span ); curl_setopt(</span><span $ch</span>, CURLOPT_RETURNTRANSFER, <span TRUE</span><span ); curl_setopt(</span><span $ch</span>, CURLOPT_URL, <span $url</span><span ); </span><span $response</span> = curl_exec(<span $ch</span><span ); curl_close(</span><span $ch</span><span ); </span><span //</span><span -------请求为空</span> <span if</span>(<span empty</span>(<span $response</span><span )){ </span><span return</span> <span false</span><span ; } </span><span return</span> <span $response</span><span ; } </span><span /*</span><span * * combineURL * 拼接url * @param string $baseURL 基于的url * @param array $keysArr 参数列表数组 * @return string 返回拼接的url </span><span */</span> <span public</span> <span function</span> combineURL(<span $baseURL</span>,<span $keysArr</span><span ){ </span><span if</span>( <span $baseURL</span>==""<span ){ </span><span $combined</span> = ""<span ; }</span><span else</span><span { </span><span $combined</span> = <span $baseURL</span>."?"<span ; } </span><span $valueArr</span> = <span array</span><span (); </span><span foreach</span>(<span $keysArr</span> <span as</span> <span $key</span> => <span $val</span><span ){ </span><span $valueArr</span>[] = "<span $key</span>=<span $val</span>"<span ; } </span><span $keyStr</span> = <span implode</span>("&",<span $valueArr</span><span ); </span><span $combined</span> .= (<span $keyStr</span><span ); </span><span return</span> <span $combined</span><span ; } } </span><span class</span> qq_openlogin <span extends</span><span openlogin{ </span><span private</span> <span $openname</span> = "qq"<span ; </span><span public</span> <span $config</span> = <span array</span><span ( </span>"appkey"=>"your appkey", "appsecret"=>"your appsecret", "redirect_uri"=>"XXXXX", "login_url" => "https://graph.qq.com/oauth2.0/authorize", "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", "authorization_url"=>"https://graph.qq.com/oauth2.0/token"<span ); </span><span function</span><span __construct() { </span><span $this</span>-><span qq_openlogin(); } </span><span function</span><span qq_openlogin(){ parent</span>::<span __construct(); } </span><span function</span><span get_access_token(){ </span><span $response</span> = parent::<span get_access_token(); </span><span /*</span><span 检测错误是否发生</span><span */</span> <span if</span>(<span strpos</span>(<span $response</span>, "callback") !== <span false</span><span ){ </span><span $lpos</span> = <span strpos</span>(<span $response</span>, "("<span ); </span><span $rpos</span> = <span strrpos</span>(<span $response</span>, ")"<span ); </span><span $response</span> = <span substr</span>(<span $response</span>, <span $lpos</span> + 1, <span $rpos</span> - <span $lpos</span> -1<span ); </span><span $msg</span> = json_decode(<span $response</span><span ); </span><span if</span>(<span isset</span>(<span $msg</span>-><span error)){ </span><span return</span> <span false</span><span ; } } </span><span $params</span> = <span array</span><span (); </span><span parse_str</span>(<span $response</span>, <span $params</span><span ); </span><span /*</span><span access_token == $params[access_token]</span><span */</span> <span /*</span><span 获取 openid </span><span */</span> <span $response</span> = <span $this</span>->get_contents("https://graph.qq.com/oauth2.0/me?access_token=".<span $params</span>['access_token'<span ]); </span><span //</span><span --------检测错误是否发生</span> <span if</span>(<span strpos</span>(<span $response</span>, "callback") !== <span false</span><span ){ </span><span $lpos</span> = <span strpos</span>(<span $response</span>, "("<span ); </span><span $rpos</span> = <span strrpos</span>(<span $response</span>, ")"<span ); </span><span $response</span> = <span substr</span>(<span $response</span>, <span $lpos</span> + 1, <span $rpos</span> - <span $lpos</span> -1<span ); } </span><span $user</span> = json_decode(<span $response</span><span ); </span><span if</span>(<span isset</span>(<span $user</span>-><span error)){ </span><span return</span> <span false</span><span ; } </span><span /*</span><span 获取用户信息需要参数:openid(用户的ID,与QQ号码一一对应),access_token(可通过使用Authorization_Code获取Access_Token 或来获取access_token有3个月有效期),oauth_consumer_key(用户appid),format(返回格式) </span><span */</span> <span /*</span><span 数据库保存</span><span */</span> <span $open_param</span> = <span array</span><span ( </span>"openid"=><span $user</span>->openid, "access_token"=><span $params</span>['access_token'<span ] ); </span><span // </span> <span $open_param</span>['oauth_consumer_key'] = <span $this</span>->config['appkey'<span ]; </span><span $open_param</span>['format'] = "json"<span ; </span><span /*</span><span 拼接url</span><span */</span> <span $get_user_url</span> = <span $this</span>->combineURL("https://graph.qq.com/user/get_user_info",<span $open_param</span><span ); </span><span //</span><span 猎取用户信息</span> <span $userinfo</span> = <span $this</span>->get_contents(<span $get_user_url</span><span ); </span><span $userinfo</span> = json_decode(<span $userinfo</span><span ); </span><span return</span> <span $userinfo</span><span ; } } </span><span class</span> weibo_openlogin <span extends</span><span openlogin{ </span><span private</span> <span $openname</span> = "weibo"<span ; </span><span public</span> <span $config</span> = <span array</span><span ( </span>"appkey"=>"your appkey", "appsecret"=>"your appsecret", "login_url" => "https://api.weibo.com/oauth2/authorize", "redirect_uri"=>"XXXXXXX", "authorization_url"=>"https://api.weibo.com/oauth2/access_token"<span ); </span><span function</span><span __construct() { </span><span $this</span>-><span qq_openlogin(); } </span><span function</span><span qq_openlogin(){ parent</span>::<span __construct(); } </span><span function</span><span get_access_token(){ </span><span $response</span> = parent::<span get_access_token(); </span><span $userinfo</span> = json_decode(<span $response</span><span ); </span><span return</span> <span $userinfo</span><span ; } } </span><span class</span> taobao_openlogin <span extends</span><span openlogin{ </span><span private</span> <span $openname</span> = "taobao"<span ; </span><span public</span> <span $config</span> = <span array</span><span ( </span>"appkey"=>"your appkey", "appsecret"=>"your appsecret", "redirect_uri"=>"XXXXX", "authorization_url"=>"https://oauth.taobao.com/token", "login_url"=>"https://oauth.taobao.com/authorize"<span ); </span><span function</span><span __construct() { </span><span $this</span>-><span qq_openlogin(); } </span><span function</span><span qq_openlogin(){ parent</span>::<span __construct(); } </span><span function</span><span get_access_token(){ </span><span $response</span> = parent::<span get_access_token(); </span><span $userinfo</span> = json_decode(<span $response</span><span ); </span><span return</span> <span $userinfo</span><span ; } } </span><span if</span>(<span $_GET</span>['openname'<span ]){ </span><span $openname</span> = <span $_GET</span>['openname']."_openlogin"<span ; </span><span $openlogin</span> = <span new</span> <span $openname</span><span (); </span><span if</span>(!<span isset</span>(<span $_REQUEST</span>['code'<span ])){ </span><span //</span><span 请求url</span> <span $url</span> = <span $openlogin</span>-><span login_url(); </span><span if</span>(!<span $url</span><span ){ </span><span echo</span> "0"<span ; </span><span exit</span><span (); } }</span><span else</span><span { </span><span if</span>(<span isset</span>(<span $_REQUEST</span>["state"]) && (<span $_SESSION</span>['state'] != <span $_REQUEST</span>["state"<span ] )){ </span><span echo</span> "1"<span ; </span><span exit</span><span (); } </span><span $rs</span> = <span $openlogin</span>-><span get_access_token(); </span><span print_r</span>( <span $rs</span><span ); } } </span>?>
添加一个人人登陆
<span class</span> renren_openlogin <span extends</span><span openlogin{ </span><span private</span> <span $openname</span> = "renren"<span ; </span><span public</span> <span $config</span> = <span array</span><span ( </span>"appid"=>"your appid", "appkey"=>"your appkey", "appsecret"=>"your secret key", "redirect_uri"=>"XXXXXX", "authorization_url"=>"https://graph.renren.com/oauth/token", "login_url"=>"https://graph.renren.com/oauth/authorize"<span ); </span><span function</span><span __construct() { </span><span $this</span>-><span qq_openlogin(); } </span><span function</span><span qq_openlogin(){ parent</span>::<span __construct(); } </span><span function</span><span get_access_token(){ </span><span $response</span> = parent::<span get_access_token(); </span><span $userinfo</span> = json_decode(<span $response</span><span ); </span><span return</span> <span $userinfo</span><span ; </span><span /*</span><span access_token:获取的Access Token; expires_in:Access Token的有效期,以秒为单位; refresh_token:用于刷新Access Token 的 Refresh Token,长期有效,不会过期; scope:Access Token最终的访问范围,既用户实际授予的权限列表(用户在授权页面时,有可能会取消掉某些请求的权限)。关于权限的具体信息请参考 </span><span */</span><span } </span><span /*</span><span 获取登陆页面URL</span><span */</span> <span public</span> <span function</span><span login_url(){ </span><span if</span>(<span empty</span>(<span $this</span>-><span config)){ </span><span return</span> <span false</span><span ; } </span><span $config</span> = <span $this</span>-><span config; </span><span $login_url</span> = <span $config</span>['login_url'<span ]; </span><span $array</span> = <span array</span><span ( </span>"response_type"=>"code", "client_id"=><span $config</span>['appid'], "redirect_uri"=><span urlencode</span>( <span $config</span>['redirect_uri'<span ] ) ); </span><span $this</span>->set(<span $array</span><span ); </span><span $url</span> = <span $this</span>->combineURL(<span $login_url</span> , <span $this</span>-><span _param); </span><span if</span>(<span $url</span><span ){ @</span><span header</span>("Location:".<span $url</span><span ); }</span><span else</span><span { </span><span return</span> <span false</span><span ; } } }</span>