微信公众平台SDK开发(三)

Wx.php

<?php
namespace wechat;
 
class Wx{
    //微信配置项
    static $config=[];
    protected $config=[];
    protected $message;
    public function __construct(array $config=[]){
        if(is_array($config)){
            self::$config=$config;
        }
        $this->message=$this->parsePostRequestData();
    }
    public function show(){
        echo 'wx';
    }
    //与微信服务器进行绑定
    public function valid(){
        $signature=$_GET["signature"];
        $timestamp=$_GET["timestamp"];
        $nonce=$_GET['nonce'];
        
        //$token='developer';
        $token=self::$config['token'];
        $tmpArr=array($token,$timestamp,$nonce);
        sort($tmpArr,SORT_STRING);
        $tmpStr=implode($tmpArr);
        $tmpStr=sha1($tmpStr);
        
        if($tmpStr==$signature){
            echo $_GET['echoStr'];
        }
    }
    //获取并解析用户发来的消息内容
    private function parsePostRequestData(){
        if(isset($_GLOBALS['HTTP_RAW_POST_DATA'])){
            //simplexml_load_string - 将XML字符串解释为对象
            return simplexml_load_string($_GLOBALS['HTTP_RAW_POST_DATA'],'SimpleXMLElement',LIBXML_NOCDATA);  // 将 CDATA 设置为文本节点
        }
    }
    //获取粉丝发来的消息内容
    public function getMessage(){
        return $this->message;
    }
    //获取功能实例类,如:消息管理
    //使用wx->instance('message');
    public function instance($name){
        $class='\wechat\build\\'.ucfirst($name);
        return new $class;
    }
}
 
 


Entry.php

<?php
//业务代码,为了测试微信sdk的功能
//微信操作的基础类 
namespace app;
use wechat\Wx;

class Entry{
    protected $wx;
    public function __construct(){
        $config=[
            'token'=>'developer'
        ];
        $this->wx=new Wx($config);
        $this->wx->valid();
    }
    public function handler(){
        //echo 'handler';
        //(new Wx())->show();
        //$content=$this->wx->getMessage();
        //file_put_contents('q.php',var_export($content,true));
        
        //回复用户信息
        /*$message=$this->wx->getMessage();
        $time=time();
        echo <<<php
        <xml> 
            <ToUserName>< ![CDATA[{$message->FromUserName}] ]></ToUserName> 
            <FromUserName>< ![CDATA[{$message->ToUserName}] ]></FromUserName> 
            <CreateTime>{$time}</CreateTime> 
            <MsgType>< ![CDATA[text] ]></MsgType> 
            <Content>< ![CDATA[这是我回复的消息] ]></Content> 
        </xml>
php;
        */
        $this->wx->instance('message')->show();        
    }
}
 


SimpleXMLElement::__set_state(array(
    'ToUserName'=>'gh_17962820bb47',
    'FromUserName'=>'odnoLwrZfu6WwdGhzoHYMhz-L8rc',
    'CreateTime'=>'1478718634',
    'MsgType'=>'text',
    'Content'=>'gghhh',
    'MsgId'=>'6351048173485048275'
))


|wechatSDK
|    |---|app
|    |---|--|Entry.php
|    |---|wechat
|    |---|--Wx.php
|    |---|bootstrap.php
|     |---|build
|     |---|---Message.php


<?php
namespace wechat\build;

//专门处理微信消息
use wechat\Wx;

class Message extends Wx{
    public function show(){
        echo __METHOD__;
    }
}

?>
 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
C#版的微信公众号开发SDK Senparc.Weixin.MP几个关键类介绍如下: Entities/Request*.cs 用于接收微信平台自动发送到服务器的实体(发送过来的是XML),包括文本、位置、图片类 Entities/Response*.cs 用于反馈给发送人的信息实体(最终会转成XML),包括文本、新闻(图文)两类 Helpers/EntityHelper.cs 用于实体和XML之间的转换(由于其中有许多需要特殊处理的字段和类型,这里不能简单用XML序列化) Helpers/MsgTypeHelper.cs 用于获取消息类型 CheckSignature.cs 验证请求合法性类 Enums.cs 各种枚举 RequestMessageFactory.cs 用于自动生成不同Request类型的实体,并作必要的数据填充 Senparc.Weixin.MP几个关键类及重要方法(按一般使用过程排序) 生成验证字符串:Senparc.Weixin.MP.CheckSignature.GetSignature(string timestamp, string nonce, string token = null),返回根据微信平台提供的数据,SHA1加密后的验证字符串(注意token必须跟公众平台的设置一直) 验证请求:Senparc.Weixin.MP.CheckSignature.Check(string signature, string timestamp, string nonce, string token = null),验证请求是否合法 获取请求实体:var requestMessage = Senparc.Weixin.MP.RequestMessageFactory.GetRequestEntity(XDocument doc); 根据不同请求的类型,自动生成可用于操作的实体(doc只需要用XDocument.Parse(xmlString)就能生成),requestMessage.MsgType就是请求枚举类型。 进行判断及各类操作。 根据需要,创建响应类型的实体,如:var responseMessage = ResponseMessageBase.CreateFromRequestMessage(requestMessage, ResponseMsgType.Text) as ResponseMessageText; 即可返回文本类型信息。 由于目前微信只接受XML的返回数据,所以在返回之前还需要做一次转换:XDocument responseDoc = Senparc.Weixin.MP.Helpers.EntityHelper.ConvertEntityToXml(responseMessage); var xmlString =responseDoc.ToString();
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值