您的位置:首页 > 移动开发 > 微信开发

微信公众平台开发者的入门教程-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;
}
}
?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: