微信公众平台开发者的入门教程-PHP源码示例
2018-03-02 09:47
603 查看
以下程序基于ThinkPHP5框架研发,基本语法通用,个别语法需要自己修改了,同时修改的过程中也会增强你的阅读能力,提高水平。开发者配置的服务器:http://www.xin-shidai.com/index/MpWeixin/indexPHP文件 MpWeixin.php<?php/*** Author: 惹妹子生气了* Date: 2017-08-10*/namespace app\index\controller;use think\Db;class MpWeixin extends Base{ public $class_obj; public $is_check_signature = false; public function __construct() { parent::__construct(); $echostr = $_GET['echostr']; // 是否来自于微信的服务器配置验证 if ($echostr) { $this->is_check_signature = true; }else{ $this->is_check_signature = false; include_once "mpweixin.class.php"; $this->class_obj = new mpweixin(); //实例化 } } /** * 服务器地址(用户消息和开发者需要的事件推送,会被转发到该URL中) */ public function index() { if ($this->is_check_signature) { $this->valid(); }else{ $this->class_obj->responseMsg(); } }
/** * 开发者对签名(即signature)的效验,来判断此条消息的真实性 */ public function valid() { $echoStr = $this->checkSignature(); if($echoStr){ header('content-type:text'); exit($echoStr); } } /** * 验证是否来自于微信 * @return [type] [description] */ public function checkSignature() { //微信会发送4个参数到我们的服务器后台 签名 时间戳 随机字符串 随机数 $signature = $_GET['signature']; // 签名 $timestamp = $_GET['timestamp']; // 时间戳 $echoStr = $_GET['echostr']; // 随机字符串 $nonce = $_GET['nonce']; // 随机数 if ($signature && $timestamp && $echoStr && $nonce) { // 微信公众号基本配置中的token $token = TOKEN; //将token、timestamp、nonce按字典序排序 $tmpArr = array($token, $timestamp, $nonce); sort($tmpArr, SORT_STRING); // 将三个参数字符串拼接成一个字符串进行sha1加密 $tmpStr = implode($tmpArr); $tmpStr = sha1($tmpStr); // 开发者获得加密后的字符串可与signature对比,标识该请求来源于微信 if($tmpStr == $signature){ return $echoStr; } else { return false; } } }}PHP微信公众号封装类 WpWeixin.class.php:<?php
/*
* 微信公众号插件
* 开发者模式,请先注册微信开放平台账号,然后启动开发模式
*/
class mpweixin {
public function responseMsg()
{
$postStr = file_get_contents("php://input");
if (!empty($postStr)){
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$RX_TYPE = trim($postObj->MsgType);
switch ($RX_TYPE)
{
case "event":
$result = $this->receiveEvent($postObj);
break;
case "text":
$result = $this->receiveText($postObj);
break;
}
echo $result;
}else {
echo "";
exit;
}
}
private function receiveEvent($object)
{
$content = "";
switch ($object->Event)
{
case "subscribe":
$content = "欢迎关注方倍工作室";
break;
case "unsubscribe":
$content = "取消关注";
break;
}
$result = $this->transmitText($object, $content);
return $result;
}
//接收文本消息
private function receiveText($object)
{
$keyword = trim($object->Content);
$content = date("Y-m-d H:i:s",time())."\n技术支持 方倍工作室";
if(is_array($content)){
if (isset($content[0]['PicUrl'])){
$result = $this->transmitNews($object, $content);
}else if (isset($content['MusicUrl'])){
$result = $this->transmitMusic($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 transmitNews($object, $arr_item)
{
if(!is_array($arr_item))
return;
$itemTpl = " <item>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
<PicUrl><![CDATA[%s]]></PicUrl>
<Url><![CDATA[%s]]></Url>
</item>
";
$item_str = "";
foreach ($arr_item as $item)
$item_str .= sprintf($itemTpl, $item['Title'], $item['Description'], $item['PicUrl'], $item['Url']);
$newsTpl = "<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($newsTpl, $object->FromUserName, $object->ToUserName, time(), count($arr_item));
return $result;
}
private function transmitMusic($object, $musicArray)
{
$itemTpl = "<Music>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
<MusicUrl><![CDATA[%s]]></MusicUrl>
<HQMusicUrl><![CDATA[%s]]></HQMusicUrl>
</Music>";
$item_str = sprintf($itemTpl, $musicArray['Title'], $musicArray['Description'], $musicArray['MusicUrl'], $musicArray['HQMusicUrl']);
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[music]]></MsgType>
$item_str
</xml>";
$result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time());
return $result;
}
}
?>
/** * 开发者对签名(即signature)的效验,来判断此条消息的真实性 */ public function valid() { $echoStr = $this->checkSignature(); if($echoStr){ header('content-type:text'); exit($echoStr); } } /** * 验证是否来自于微信 * @return [type] [description] */ public function checkSignature() { //微信会发送4个参数到我们的服务器后台 签名 时间戳 随机字符串 随机数 $signature = $_GET['signature']; // 签名 $timestamp = $_GET['timestamp']; // 时间戳 $echoStr = $_GET['echostr']; // 随机字符串 $nonce = $_GET['nonce']; // 随机数 if ($signature && $timestamp && $echoStr && $nonce) { // 微信公众号基本配置中的token $token = TOKEN; //将token、timestamp、nonce按字典序排序 $tmpArr = array($token, $timestamp, $nonce); sort($tmpArr, SORT_STRING); // 将三个参数字符串拼接成一个字符串进行sha1加密 $tmpStr = implode($tmpArr); $tmpStr = sha1($tmpStr); // 开发者获得加密后的字符串可与signature对比,标识该请求来源于微信 if($tmpStr == $signature){ return $echoStr; } else { return false; } } }}PHP微信公众号封装类 WpWeixin.class.php:<?php
/*
* 微信公众号插件
* 开发者模式,请先注册微信开放平台账号,然后启动开发模式
*/
class mpweixin {
public function responseMsg()
{
$postStr = file_get_contents("php://input");
if (!empty($postStr)){
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$RX_TYPE = trim($postObj->MsgType);
switch ($RX_TYPE)
{
case "event":
$result = $this->receiveEvent($postObj);
break;
case "text":
$result = $this->receiveText($postObj);
break;
}
echo $result;
}else {
echo "";
exit;
}
}
private function receiveEvent($object)
{
$content = "";
switch ($object->Event)
{
case "subscribe":
$content = "欢迎关注方倍工作室";
break;
case "unsubscribe":
$content = "取消关注";
break;
}
$result = $this->transmitText($object, $content);
return $result;
}
//接收文本消息
private function receiveText($object)
{
$keyword = trim($object->Content);
$content = date("Y-m-d H:i:s",time())."\n技术支持 方倍工作室";
if(is_array($content)){
if (isset($content[0]['PicUrl'])){
$result = $this->transmitNews($object, $content);
}else if (isset($content['MusicUrl'])){
$result = $this->transmitMusic($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 transmitNews($object, $arr_item)
{
if(!is_array($arr_item))
return;
$itemTpl = " <item>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
<PicUrl><![CDATA[%s]]></PicUrl>
<Url><![CDATA[%s]]></Url>
</item>
";
$item_str = "";
foreach ($arr_item as $item)
$item_str .= sprintf($itemTpl, $item['Title'], $item['Description'], $item['PicUrl'], $item['Url']);
$newsTpl = "<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($newsTpl, $object->FromUserName, $object->ToUserName, time(), count($arr_item));
return $result;
}
private function transmitMusic($object, $musicArray)
{
$itemTpl = "<Music>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
<MusicUrl><![CDATA[%s]]></MusicUrl>
<HQMusicUrl><![CDATA[%s]]></HQMusicUrl>
</Music>";
$item_str = sprintf($itemTpl, $musicArray['Title'], $musicArray['Description'], $musicArray['MusicUrl'], $musicArray['HQMusicUrl']);
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[music]]></MsgType>
$item_str
</xml>";
$result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time());
return $result;
}
}
?>
相关文章推荐
- php版微信公众平台入门教程之开发者认证的方法
- 微信公众平台开发教程(四) 实例入门:机器人(附源码)
- PHP快速入门教程:WHILE循环示例
- PHP快速入门教程:WHILE循环示例
- Java API——RMIIO入门教程(2)远程流传输示例之RMIIO服务源码
- PHP快速入门教程:WHILE循环示例
- Java API——RMIIO入门教程(3)远程流传输示例之RMIIO服务器端源码
- PHP快速入门教程:服务器和浏览器变量获取示例
- PHP快速入门教程:WHILE循环的使用示例
- 微信公众平台开发教程(四) 实例入门:机器人(附源码)
- PHP快速入门教程:WHILE循环的使用示例
- PHP快速入门教程:WHILE循环的使用示例
- AngularJS入门教程之与服务器(Ajax)交互操作示例【附完整demo源码下载】
- 微信公众平台开发教程之新手初级入门攻略 附PHP代码实例
- 微信公众平台开发教程(四) 实例入门:机器人回复(附源码)
- 【备忘】【No5】微信公众平台开发入门到实战开发视频教程(Java+PHP)
- 微信公众平台开发教程(四) 实例入门:机器人(附源码)
- Java API——RMIIO入门教程(2)远程流传输示例之RMIIO服务源码
- 微信公众平台开发教程(四) 实例入门:机器人(附源码)