微信公众平台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__;
    }
}

?>
 

猜你喜欢

转载自blog.csdn.net/taotaobaobei/article/details/81429405