搜索
系统检测到您的用户名不符合规范:

微信基本操作的封装

浏览:1435 发布日期:2016年10月28日 分类:系统代码 关键字: weixin
关于微信服务器验证,获取用户基本信息,自定义菜单,发送模板消息封装为一个类文件,方便直接调用
<?php
class weixin
{
private $appId = "";
private $appSecrect = "";
private $redirect_uri = $this->get_user_accessToken;

//微信服务器验证
public function weixin_token()
{
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];
$token = "token";
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}

// 微信用户授权code
public function get_code($state)
{
$redirect_uri = urlencode($this->redirect_uri);
$code_url = "https://open.weixin.qq.com/connect/oauth2/authorize?appid={$this->appId}&redirect_uri={$this->redirect_uri}&response_type=code&scope=snsapi_userinfo&state={$state}#wechat_redirect";
header("Location:{$code_url}");
}

// 获取access_token(微信用户授权)
public function get_user_accessToken()
{
$code = $_GET['code'];
$url = "https://api.weixin.qq.com/sns/oauth2/access_token?appid={$this->$appId}&secret={$this->appSecrect}&code={$code}&grant_type=authorization_code";
$result_data = $this->http($url);
$result = json_decode($result_data, true);
$this->get_userinfo($result['access_token'], $result['openid']);

}

// 获取用户的基本信息
public function get_userinfo($user_access_token,$open_id)
{
$url = "https://api.weixin.qq.com/sns/userinfo?access_token={$user_access_token}&openid={$open_id}&lang=zh_CN";
$result_data = $this->http($url);
$result = json_decode($result_data, true);
return $result;
}

// 获取access_token(接口调用)
public function access_token()
{
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid={$this->appId}&secret={$this->appSecrect}";
$result_data = $this->http($url);
$result = json_decode($result_data, true);
return $result['access_token'];

}

// 创建菜单
public function create_menu()
{
$access_token = $this->access_token();
// 如果菜单类型是view的,url的值不能太长
$menu_content = '{
"button":[
{
"type":"click",
"name":"今日歌曲",
"key":"V1001_TODAY_MUSIC"
},
{
"name":"菜单",
"sub_button":[
{
"type":"view",
"name":"搜索",
"url":"http://www.soso.com/"
},
{
"type":"view",
"name":"视频",
"url":"http://v.qq.com/"
},
{
"type":"click",
"name":"赞一下我们",
"key":"V1001_GOOD"
}]
}]
}';
$url = "https://api.weixin.qq.com/cgi-bin/menu/create?access_token={$access_token}";
$result = $this->http($url, $menu_content);
return $result;
}

// 发送模板消息
public function send_template_mesages()
{
$access_token = $this->access_token();
$url = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token={$access_token}";
// 这里的first、keynote1。。。 是在配置模板信息的格式{{first.DATA}}
$template_content = '{
"touser":"OPENID",
"template_id":"ngqIpbwh8bUfcSsECmogfXcV14J0tQlEpBO27izEYtY",
"url":"http://weixin.qq.com/download",
"data":{
"first": {
"value":"恭喜你购买成功!",
"color":"#173177"
},
"keynote1":{
"value":"巧克力",
"color":"#173177"
},
"keynote2": {
"value":"39.8元",
"color":"#173177"
},
"keynote3": {
"value":"2014年9月22日",
"color":"#173177"
},
"remark":{
"value":"欢迎再次购买!",
"color":"#173177"
}
}
}';
$result = $this->http($url, $template_content);
return $result;
}

// curl工具
public function http($url,$data = null){
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);
if (!empty($data)){
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $data);
}
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$output = curl_exec($curl);
curl_close($curl);
return $output;
}

}
评论() 相关
收藏
太平洋警察
积分:1321 等级:LV3
热点推荐
(追記) (追記ここまで)
最新更新

我们

合作

网站

信息

ThinkPHP 是一个免费开源的,快速、简单的面向对象的 轻量级PHP开发框架 ,创立于2006年初,遵循Apache2开源协议发布,是为了敏捷WEB应用开发和简化企业应用开发而诞生的。ThinkPHP从诞生以来一直秉承简洁实用的设计原则,在保持出色的性能和至简的代码的同时,也注重易用性。并且拥有众多的原创功能和特性,在社区团队的积极参与下,在易用性、扩展性和性能方面不断优化和改进,已经成长为国内最领先和最具影响力的WEB应用开发框架,众多的典型案例确保可以稳定用于商业以及门户级的开发。

AltStyle によって変換されたページ (->オリジナル) /