微信網頁授權獲取使用者資訊(ThinkPHP5)+ 微信傳送客服訊息(一)
以thinkphp5為例項,建立控制器
class Kf extends Controller
{
/**
* [protected description]微信公眾號appid
* @var [type]
*/
protected $appid = "xxxxxxxxxxxxxxx";
/**
* [protected description]微信公眾號appsecret
* @var [type]
*/
protected $appsecret = "xxxxxxxxxxxxxxxxx";
/**
* [protected description]微信公眾號回撥地址
* @var [type]
*/
protected $redirect_uri = "https://xxxxx.xxxxxxx.cn/kf/getAccessToken?type=bw";
/**
* [getAccessToken description]通過code獲取使用者資訊(網頁授權方式)
* @method getAccessToken
* @return [type] [description]
*/
public function getInfo($appid,$appsecret,$code)
{
$urls = "https://api.weixin.qq.com/sns/oauth2/access_token?appid=".$appid.
"&secret=".$appsecret.
"&code=".$code.
"&grant_type=authorization_code";
$data = $this->http_curl($urls); //得到使用者的access_token和openid
$data = json_decode($data,true);
return $data;
}
/**
* [getUserInfo description]獲取使用者資訊
* @method getUserInfo
* @return [type] [description]
*/
public function getUserInfo($AccessToken, $OpenId)
{
$url = "https://api.weixin.qq.com/sns/userinfo?access_token=".$AccessToken."&openid=".$OpenId."&lang=zh_CN";
$result = $this->http_curl($url);
return $result;
}
/**
* [getAccessToken description]主頁面, 回撥獲取code
* @method getAccessToken
* @return [type] [description]
*/
public function getAccessToken(){
//1、去微信獲取code 微信再重定向到當前介面
if (!isset($_GET['type'])) {
$url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid=".$this->appid.
"&redirect_uri=".$this->redirect_uri.
"&response_type=code&scope=snsapi_base&state=STATE#wechat_redirect";
$this->redirect($url);
}
$code = $_GET['code'];
//2.微信重定向回來。 用code 獲取使用者資訊。這兒就能獲取到code
if(null != $code){
$data = $this->getInfo($this->appid,$this->appsecret,$code);
// var_dump($data);die;
$UserInfo = $this->getUserInfo($data['access_token'], $data['openid']);
$UserInfo = json_decode($UserInfo,true);
// var_dump($UserInfo);die;
//到此出為止,能獲取到使用者的基本資訊
Session::set("wx.OpenId", $UserInfo['openid']);
Session::set("wx.NickName", $UserInfo['nickname']);
$this->redirect("https://xxxx.xxxxx.cn/kf/index?kf_access_token=".$UserInfo['openid']);
}
}
//業務頁面方法(html程式碼見最後)
public function index()
{
$openid = $_GET['kf_access_token'];
if(null != $openid){
return $this->fetch();
}else{
$this->error("OpenId系統錯誤 , 請聯絡公司客服");
}
}
//點擊發送微信客服訊息 ,圖文訊息
public function SendInfo()
{
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$this->appid."&secret=".$this->appsecret;
$res = $this->http_curl($url);
$res = json_decode($res,true);
// var_dump($res);die;
$urls = "https://api.weixin.qq.com/cgi-bin/message/custom/send?access_token=".$res['access_token'];
$nickname = Session::get('wx.NickName');
$data = '{
"touser":"'.Session::get('wx.OpenId').'",
"msgtype":"text",
"text":
{
"content":"大家好我是測試訊息~你應該是:'.$nickname.'"
}
}';
$list = $this->http_curl2($urls,$data);
var_dump($list);die;
}
//功能:curl通訊
private function http_curl ($url)
{
$ch = curl_init();//初始化
curl_setopt($ch, CURLOPT_URL, $url);//初始化curl連線地址,設定要抓取的url
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//設定將得到的結果儲存在字串中還是輸出在螢幕上,0為直接輸出螢幕,非0則不輸出
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//對認證證書來源的檢查,false表示阻止檢查
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);//從證書中檢查ssl加密演算法是否存在
curl_setopt($ch,CURLINFO_HEADER_OUT,true);
// curl_setopt ( $ch, CURLOPT_POST, 1 );
// curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data );
$result = curl_exec($ch);//執行請求獲取返回結果
// $info = curl_getinfo($ch);
curl_close($ch);//關閉curl會話
return $result;
}
private function http_curl2 ($url,$data)
{
$ch = curl_init();//初始化
curl_setopt($ch, CURLOPT_URL, $url);//初始化curl連線地址,設定要抓取的url
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//設定將得到的結果儲存在字串中還是輸出在螢幕上,0為直接輸出螢幕,非0則不輸出
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);//對認證證書來源的檢查,false表示阻止檢查
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);//從證書中檢查ssl加密演算法是否存在
curl_setopt($ch,CURLINFO_HEADER_OUT,true);
curl_setopt ( $ch, CURLOPT_POST, 1 );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data );
$result = curl_exec($ch);//執行請求獲取返回結果
// $info = curl_getinfo($ch);
curl_close($ch);//關閉curl會話
return $result;
}
}
//html頁面程式碼,很簡單的一個a標籤
<!DOCTYPE html>
<html lang="en" dir="ltr">
<head>
<meta charset="utf-8">
<title></title>
</head>
<body>
//此處放置按鈕點擊發送客服訊息(文字訊息)
<a href="{:url('kf/SendInfo')}">傳送訊息</a>
</body&