php獲取微信openid方法總結(jié)
使用微信接口,無論是自動(dòng)登錄還是微信支付我們首先需要獲取的就是openid,獲取openid的方式有兩種,一種是在關(guān)注的時(shí)候進(jìn)行獲取,這種訂閱號(hào)就可以獲取的到,第二種是通過網(wǎng)頁授權(quán)獲取,這種獲取需要的是認(rèn)證服務(wù)號(hào)。
今天我要說的是第二種網(wǎng)頁授權(quán)獲取openid。下面是我寫的一個(gè)關(guān)于獲取openid的類
<?php
/**
* 微信授權(quán)相關(guān)接口
*
* @link http://www.phpddt.com
*/
class Wchat
{
private $app_id = 'wx444444444444';
private $app_secret = '77777777';
private $state='aaaa';
/**
* 獲取微信授權(quán)鏈接
*
* @param string $redirect_uri 跳轉(zhuǎn)地址
* @param mixed $state 參數(shù)
*/
public function get_authorize_url($redirect_uri = '', $state = '')
{
$redirect_uri = urlencode($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";
}
/**
* 獲取微信openid
*/
public function getOpenid($turl)
{
if (!isset($_GET['code'])){
//觸發(fā)微信返回code碼
$url=$this->get_authorize_url($turl, $this->state);
Header("Location: $url");
exit();
} else {
//獲取code碼,以獲取openid
$code = $_GET['code'];
$access_info = $this->get_access_token($code);
return $access_info;
}
}
/**
* 獲取授權(quán)token網(wǎng)頁授權(quán)
*
* @param string $code 通過get_authorize_url獲取到的code
*/
public function get_access_token($code = '')
{
$appid=$this->app_id;
$appsecret=$this->app_secret;
$token_url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid."&secret=".$appsecret."&code=".$code."&grant_type=authorization_code";
//echo $token_url;
$token_data = $this->http($token_url);
// var_dump( $token_data);
if($token_data[0] == 200)
{
$ar=json_decode($token_data[1], TRUE);
return $ar;
}
return $token_data[1];
}
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);
}
}
?>
getOpenid($turl)這個(gè)方法就是獲取openid的方法。前端調(diào)用代碼如下:
$openid=isset($_COOKIE['openid'])?$_COOKIE['openid']:'';
if(empty($openid))
{
$wchat=new wchat();
$t_url='http://'.$_SERVER['HTTP_HOST'].'/user.php?act=register';
$info=$wchat->getOpenid($t_url);
if($info){
$openid=$info['openid'];
setcookie('openid',$openid,time()+86400*30);
}
}
以上就是我總結(jié)的獲取openid的方法啦。
以上就是php獲取微信openid的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Linux平臺(tái)php命令行程序處理管道數(shù)據(jù)的方法
這篇文章主要介紹了Linux平臺(tái)php命令行程序處理管道數(shù)據(jù)的方法,結(jié)合實(shí)例形式分析了Linux平臺(tái)管道提示符的功能及php使用命令行處理管道數(shù)據(jù)的相關(guān)實(shí)現(xiàn)技巧,需要的朋友可以參考下2016-11-11
php圖片處理函數(shù)獲取類型及擴(kuò)展名實(shí)例
這篇文章主要介紹了php圖片處理函數(shù)獲取類型及擴(kuò)展名的方法,包括image2wbmp、image_type_to_extension、image_type_to_mime_type等函數(shù)的具體使用,具有不錯(cuò)的借鑒與學(xué)習(xí)價(jià)值,需要的朋友可以參考下2014-11-11
PHP連接MySql數(shù)據(jù)庫(kù)方法簡(jiǎn)化版
MySQL是一個(gè)關(guān)系型數(shù)據(jù)庫(kù)管理系統(tǒng),由瑞典MySQL?AB?公司開發(fā),屬于?Oracle?旗下產(chǎn)品。MySQL?是最流行的關(guān)系型數(shù)據(jù)庫(kù)管理系統(tǒng)之一,這篇文章主要介紹了PHP連接mysql數(shù)據(jù)庫(kù),數(shù)據(jù)庫(kù)連接靜態(tài)工具類,簡(jiǎn)化連接2022-07-07
PHP使用trim函數(shù)去除字符串左右空格及特殊字符實(shí)例
這篇文章主要介紹了PHP使用trim函數(shù)去除字符串左右空格及特殊字符的用法,結(jié)合實(shí)例簡(jiǎn)單分析了trim函數(shù)不帶附加參數(shù)去除空格及使用附加參數(shù)去除指定字符的使用技巧,需要的朋友可以參考下2016-01-01
PHP利用MySQL保存session的實(shí)現(xiàn)思路及示例代碼
使用MySQL保存session,需要保存三個(gè)關(guān)鍵性的數(shù)據(jù):session id、session數(shù)據(jù)、session生命期,下面的示例,大家可以看看2014-09-09

