以thinkphp5为实例,建立控制器php
class Kf extends Controller
{
/**
* [protected description]微信公众号appid
* @var [type]
*/
protected $appid = "xxxxxxxxxxxxxxx";html
/**
* [protected description]微信公众号appsecret
* @var [type]
*/
protected $appsecret = "xxxxxxxxxxxxxxxxx";算法
/**
* [protected description]微信公众号回调地址
* @var [type]
*/
protected $redirect_uri = "https://xxxxx.xxxxxxx.cn/kf/getAccessToken?type=bw";thinkphp
/**
* [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";json
$data = $this->http_curl($urls); //获得用户的access_token和openid
$data = json_decode($data,true);
return $data;
}api
/**
* [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(){
//一、去微信获取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);
}app
$code = $_GET['code'];curl
//2.微信重定向回来。 用code 获取用户信息。这儿就能获取到code
if(null != $code){thinkphp5
$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> </html>