微信公众平台开发接口PHP SDK
以前没接触过微信公众平台开发,前几天刚找到实习公司就要求我做一个微信公众平台的应用,于是乎开始学习微信公众平台开发接口的调用,看开发文档之后还是不知道从何入手,只好上网找入门资料,终于在方倍工作室找到了完整的SDK,理解了思路之后其实也挺简单的,无非就是进行用户、微信服务器、开发者服务器三者之间的通信。下面是我参考方倍工作室SDK之后之间写的一个SDK,主要是响应文本消息和CLICK按钮消息,获取用户基本信息,当然并没有包含所有的消息类型和事件。更完整的SDK请参考方倍工作室,链接:http://www.cnblogs.com/txw1958/p/weixin-php-sdk.html。
$token = 'qiduoyun';
$appid = 公众号appid;
$appsecret = 公众号appsecret;
$wechat = new Wechat($token,$appid,$appsecret);
if(!isset($_GET['echostr'])) {
$wechat->responseMsg();
} else {
$wechat->valid();
}
class Wechat
{
private $token;
private $appid;
private $appsecret;
public function __construct($token,$appid,$appsecret)
{
$this->token = $token;
$this->appid = $appid;
$this->appsecret = $appsecret;
}
//验证消息真实性
public function valid()
{
$echostr = $_GET['echostr'];
if($this->checkSignature()) {
echo $echostr;
exit;
}
}
//验证签名
private function checkSignature()
{
$token = $this->token;
$timestamp = $_GET['timestamp'];
$nonce = $_GET['nonce'];
$signature = $_GET['signature'];
$tmpArr = array($token,$timestamp,$nonce);
sort($tmpArr,SORT_STRING);
$tmpStr = implode($tmpArr);
$tmpStr = sha1($tmpStr);
if($tmpStr == $signature) {
return true;
} else {
return false;
}
}
//获取access_token
private function get_access_token($appid,$appsecret)
{
$url = "https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=" .
$appid . "&secret=" . $appsecret;
$output = file_get_contents($url);
$jsoninfo = json_decode($output,true);
$access_token = $jsoninfo['access_token'];
return $access_token;
}
//响应消息
public function responseMsg()
{
$postStr = $GLOBALS['HTTP_RAW_POST_DATA'];
if(!empty($postStr)) {
$postObj = simplexml_load_string($postStr,'SimpleXMLElement',LIBXML_NOCDATA);
$RX_TYPE = trim($postObj->MsgType);
switch($RX_TYPE)
{
case 'text':
$result = $this->receiveText($postObj);
break;
case 'event':
$result = $this->receiveEvent($postObj);
break;
default:
$result = "unknown message type: " . $RX_TYPE;
break;
}
echo $result;
} else {
echo '';
exit;
}
}
//接收文本消息
private function receiveText($object)
{
switch($object->Content)
{
case '猜猜我是谁': //发送单图文消息:用户基本信息
//获取access_token
$access_token = $this->get_access_token($this->appid,$this->appsecret);
//获取用户信息
$url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=" .
$access_token . "&openid=" . $object->FromUserName . "&lang=zh_CN";
$output = file_get_contents($url);
$jsoninfo = json_decode($output,true);
if($jsoninfo['sex'] == 1) {
$sex = "男";
} else if($jsoninfo['sex'] == 2) {
$sex = "女";
} else {
$sex = "未知";
}
$content = array();
$content[] = array('Title' => "我知道你是谁!",'Description' => "昵称:" . $jsoninfo['nickname'] .
"\r\n" . "性别:" . $sex . "\r\n" . "国家:" . $jsoninfo['country'] . "\r\n" . "省份:" .
$jsoninfo['province'] . "\r\n" . "城市:" . $jsoninfo['city'],'PicUrl' => $jsoninfo['headimgurl'],
'Url' => '');
break;
default:
$content = '您发送的消息有误!';
break;
}
if(is_array($content)) {
if(isset($content[0]['PicUrl'])) {
$result = $this->transmitInfo($object,$content);
}
} else {
$result = $this->transmitText($object,$content);
}
return $result;
}
//接收事件推送
private function receiveEvent($object)
{
$content = "";
switch($object->Event)
{
case 'subscribe':
$content = "欢迎关注*****!";
break;
case 'unsubscribe':
$content = "取消关注";
break;
case 'CLICK':
switch($object->EventKey)
{
case '我是谁':
//获取access_token
$access_token = $this->get_access_token($this->appid,$this->appsecret);
//获取用户信息
$url = "https://api.weixin.qq.com/cgi-bin/user/info?access_token=" .
$access_token . "&openid=" . $object->FromUserName . "&lang=zh_CN";
$output = file_get_contents($url);
$jsoninfo = json_decode($output,true);
if($jsoninfo['sex'] == 1) {
$sex = "男";
} else if($jsoninfo['sex'] == 2) {
$sex = "女";
} else {
$sex = "未知";
}
$content = array();
$content[] = array('Title' => "我知道你是谁!",'Description' => "昵称:" . $jsoninfo['nickname'] .
"\r\n" . "性别:" . $sex . "\r\n" . "国家:" . $jsoninfo['country'] . "\r\n" . "省份:" .
$jsoninfo['province'] . "\r\n" . "城市:" . $jsoninfo['city'],'PicUrl' => $jsoninfo['headimgurl'],
'Url' => '');
break;
default:
$content = "该按钮暂时尚未添加事件!";
break;
}
break;
default:
$content = "对不起,目前暂不受理此事件!";
break;
}
if(is_array($content)) {
if(isset($content[0]['PicUrl'])) {
$result = $this->transmitInfo($object,$content);
}
} else {
$result = $this->transmitText($object,$content);
}
return $result;
}
//发送文本消息
private function transmitText($object,$content)
{
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
$result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content);
return $result;
}
//发送单图文消息:用户基本信息
private function transmitInfo($object,$infoArray)
{
if(!is_array($infoArray)) {
return;
}
$itemTpl = "<item>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
<PicUrl><![CDATA[%s]]></PicUrl>
<Url><![CDATA[%s]]></Url>
</item> ";
$item_str = "";
foreach ($infoArray as $item){
$item_str .= sprintf($itemTpl, $item['Title'], $item['Description'], $item['PicUrl'],
$item['Url']);
}
$infoTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<Content><![CDATA[]]></Content>
<ArticleCount>%s</ArticleCount>
<Articles> $item_str</Articles>
</xml>";
$result = sprintf($infoTpl, $object->FromUserName, $object->ToUserName, time(),
count($infoArray));
return $result;
}
}
微信公众平台开发接口PHP SDK的更多相关文章
- 微信公众平台开发接口PHP SDK完整版(转载)
<?php/* 方倍工作室 http://www.fangbei.org/ CopyRight 2015 All Rights Reserved*/ define("TOK ...
- 微信公众平台开发接口PHP SDK完整版
<?php /* 方倍工作室 http://www.fangbei.org/ CopyRight 2015 All Rights Reserved */ define("TOKEN&q ...
- .Net实现微信公众平台开发接口(一) 之 “微信开发配置”
我们只要通过微信官方认证,成为开发者,才能实现微信提供的各种接口,否则即使调用了接口,微信也不会实现推送,功能也无法通过开发模式真正得到实现,所以需要正确配置微信信息,通过微信官方认证,成为开发者才可 ...
- .Net实现微信公众平台开发接口(二) 之 “获取access_token”
access_token是公众号的全局唯一票据,公众号调用各接口时都需使用access_token. 接口调用请求说明 http请求方式: GET https://api.weixin.qq.com/ ...
- 170303、PHP微信公众平台开发接口 SDK完整版
<?php /* 方倍工作室 http://www.fangbei.org/ CopyRight 2015 All Rights Reserved */ define("TOKEN&q ...
- .Net实现微信公众平台开发接口(三) 之 “信息回复”
对于每一个POST请求,开发者在响应包(Get)中返回特定XML结构,对该消息进行响应(现支持回复文本.图片.图文.语音.视频.音乐).请注意,回复图片等多媒体消息时需要预先上传多媒体文件到微信服务器 ...
- php的微信公众平台开发接口类
<?php define("TOKEN", "weixin"); $wechatObj = new wechatCallbackapiTest(); if ...
- 微信公众平台开发教程--方培工作室,PHP语言版本
准备工作 微信公众平台的注册 介绍如何注册一个微信公众账号. 入门教程 微信公众平台开发入门教程 内容:1.申请SAE作为服务器; 2.启用开发模式; 3.微信公众平台PHP SDK; 4.接收发送消 ...
- Senparc.Weixin.MP SDK 微信公众平台开发教程(十七):个性化菜单接口说明
前不久微信上线了个性化菜单接口,Senparc.Weixin SDK也已经同步更新. 本次更新升级Senparc.Weixin.MP版本到v13.5.2,依赖Senparc.Weixin版本4.5.4 ...
随机推荐
- video标签
Video标签的使用 Video标签含有src.poster.preload.autoplay.loop.controls.width.height等几个属性, 以及一个内部使用的标签<sour ...
- iScroll在谷歌浏览器中的问题
通常情况下,我们会使用iScroll.js做移动端的下拉刷新和上拉加载功能,当然,还有很多其他功能. 不过,在使用iScroll的时候,在谷歌浏览器中出现不支持的情况,即,做移动的时候,出现卡顿或是每 ...
- 06jQuery-03-选择器查找和过滤
1.查找 find().parent().prev().next() 通常情况下选择器可以直接定位到我们想要的元素,但是,当我们拿到一个jQuery对象后,还可以以这个对象为基准,进行查找和过滤. 最 ...
- String类的构造方法(2)
写了常见的几个而已. 1:new 一个String类的时候系统会自动传一个空构造 public String(); 注意: 当对象初始化是 null时 和 对象是 "" 时,两者是 ...
- jQuery自定义插件--banner图滚动
前言 jQuery是一个功能强大的库,提供了开发JavaScript项目所需的所有核心函数.很多时候我们使用jQuery的原因就是因为其使用插件的功能,然而,有时候我们还是需要使用自定义代码来扩展这些 ...
- 判断字符串中是否包含指定的内容&&字符串截取方法比较说明
1.使用indexOf()方法 方法说明: 作用:indexOf() 方法可返回某个指定的字符串值在字符串中首次出现的位置(从前向后查找). 语法:stringObject.indexOf(searc ...
- Opengl4.5 中文手册—E
索引 A B C D E F G H I J K L M N O P Q ...
- 使用javaAPI操作hdfs
欢迎到https://github.com/huabingood/everyDayLanguagePractise查看源码. 一.构建环境 在hadoop的安装包中的share目录中有hadoop所有 ...
- 关于Java里面File类创建txt文件重复???
private JButton getOpenButton() { if (openButton == null) { openButton = new JButton(); openButton.s ...
- 项目发布Debug和Release版的区别
一.Debug和Release的区别 Debug:调试版本,包含调试信息,所以容量比Release大很多,并且不进行任何优化(优化会使调试复杂化,因为源代码和生成的指令间关系会更复杂),便于程序员调试 ...