微信网页开发教程 php,PHP实现微信网页授权开发教程,php授权教程_PHP教程
PHP實現微信網頁授權開發教程,php授權教程
微信網頁授權是服務號才有的高級功能,開發者可以通過授權后獲取用戶的基本信息;在此之前,想要獲取消息信息只能在用戶和公眾號交互時根據openid獲取用戶信息;而微信網頁授權可在不需要消息交互,也不需要關注的情況下獲取用戶的基本信息。
微信網頁授權時通過OAuth2.0完成的,整個過程分為三步:
用戶授權,獲取code;
根據code獲取access_token【可通過refresh_token刷新獲取較長有效期】
通過access_token和openid獲取用戶信息
對微信網頁授權過程做了簡單封裝:
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($app_id = '', $app_secret = '', $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_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);
}
}
以上就是本文的全部內容,希望對大家的學習有所幫助。
您可能感興趣的文章:PHP 程序授權驗證開發思路
新浪微博API開發簡介之用戶授權(PHP基礎篇)
php使用Cookie控制訪問授權的方法
php實現paypal 授權登錄
weiphp微信公眾平臺授權設置
http://www.bkjia.com/PHPjc/1093698.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/1093698.htmlTechArticlePHP實現微信網頁授權開發教程,php授權教程 微信網頁授權是服務號才有的高級功能,開發者可以通過授權后獲取用戶的基本信息;在此之前...
總結
以上是生活随笔為你收集整理的微信网页开发教程 php,PHP实现微信网页授权开发教程,php授权教程_PHP教程的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: html调用接口_Spring 自调用事
- 下一篇: 2020年周数和日期对应表_2020年雅