php微信登录代理转发,PHP微信网页授权登录

PHP微信网页授权登录

2018年04月10日 15:44:03阅读数:601

namespace Org\WeChat;

/**

* 微信授权相关接口

*/

class Wechat {

//高级功能-》开发者模式-》获取

private $app_id = ''; //公众号appid

private $app_secret = ''; //公众号app_secret

private $redirect_uri = ''; //授权之后跳转地址

/**

* 获取微信授权链接

*

* @param string $redirect_uri 跳转地址

* @param mixed $state 参数

*/

public function get_authorize_url($state)

{

$redirect_uri = urlencode($this->redirect_uri);

return "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$this->app_id}&redirect_uri={$redirect_uri}&response_type=code&scope=snsapi_userinfo&state={$state}#wechat_redirect";

}

/**

* 获取授权token

*

* @param string $code 通过get_authorize_url获取到的code

*/

public function get_access_token($code)

{

$token_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$this->app_id}&secret={$this->app_secret}&code={$code}&grant_type=authorization_code";

$token_data = $this->http($token_url);

if($token_data[0] == 200)

{

return json_decode($token_data[1], TRUE);

}

return FALSE;

}

/**

* 获取授权后的微信用户信息

*

* @param string $access_token

* @param string $open_id

*/

public function get_user_info($access_token,$open_id)

{

if($access_token && $open_id)

{

$info_url = "https://api.weixin.qq.com/sns/userinfo?access_token={$access_token}&openid={$open_id}&lang=zh_CN";

$info_data = $this->http($info_url);

if($info_data[0] == 200)

{

return json_decode($info_data[1], TRUE);

}

}

return FALSE;

}

public function http($url, $method, $postfields = null, $headers = array(), $debug = false)

{

$ci = curl_init();

/* Curl settings */

curl_setopt($ci, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);

curl_setopt($ci, CURLOPT_CONNECTTIMEOUT, 30);

curl_setopt($ci, CURLOPT_SSL_VERIFYPEER, FALSE);

curl_setopt($ci, CURLOPT_SSL_VERIFYHOST, FALSE);

curl_setopt($ci, CURLOPT_TIMEOUT, 30);

curl_setopt($ci, CURLOPT_RETURNTRANSFER, true);

switch ($method) {

case 'POST':

curl_setopt($ci, CURLOPT_POST, true);

if (!empty($postfields)) {

curl_setopt($ci, CURLOPT_POSTFIELDS, $postfields);

$this->postdata = $postfields;

}

break;

}

curl_setopt($ci, CURLOPT_URL, $url);

curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);

curl_setopt($ci, CURLINFO_HEADER_OUT, true);

$response = curl_exec($ci);

$http_code = curl_getinfo($ci, CURLINFO_HTTP_CODE);

if ($debug) {

echo "=====post data======\r\n";

var_dump($postfields);

echo '=====info=====' . "\r\n";

print_r(curl_getinfo($ci));

echo '=====$response=====' . "\r\n";

print_r($response);

}

curl_close($ci);

return array($http_code, $response);

}

}

全部评论

文明上网理性发言,请遵守新闻评论服务协议0条评论

47d507a036d4dd65488c445c0974b649.png

立即提交

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值