微信基本操作的封装

浏览:1349 发布日期: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;
}

}
评论( 相关
后面还有条评论,点击查看>>