Home  >  Article  >  php教程  >  微信公众平台开发人员POSTXML助手[php版]

微信公众平台开发人员POSTXML助手[php版]

WBOY
WBOYOriginal
2016-06-21 08:49:341322browse

估计网上搜索有微信开发不返回信息的,各种问题出现,但是是服务端模拟的,没法测试,今天我就写了一个类,专门测试微信开发中遇到的问题,这个类使用简单,并且能够模拟提交,返回模拟的微信服务器返回的信息!这样开发起来就很简单明了了!需要的可以拿走!

 

<?php /*
 *@author widuu
 *@time 2013-7-4
 *@模拟提交测试微信数据
 */
class WinXinPost{
    private $event = "";
    private $content = "";
    private $time;
    
    /*
     *使用严格遵守微信公众平台参数配置http://mp.weixin.qq.com/wiki/index.php?title=消息接口指南
     *如果是text或者image类型就直接输入$content
     *其他的就输入array 譬如地理位置输入
     *<Location_X>23.134521
     *<location_y>113.358803</location_y>
     *   <scale>20</scale>
     * <label></label>
     * array('1.29290','12.0998','20','位置信息');
     *
     */
    public function __construct($event,$url,$content){
        $this-> event = $event;
        $this-> url = $url;
        $this-> content = $content;
        $this-> time = time();
    }

    //返回接收的消息
    public function result(){
        $postObj = simplexml_load_string($this->post(), 'SimpleXMLElement', LIBXML_NOCDATA);
        foreach ((array)$postObj as $key => $value) {
                $str.=$key.'=>'.$value."<br>";
        }
        return $str;

    }

    //处理成xml数据
    private  function xml_data(){
        $str = "
            <xml>
                 <tousername>100012</tousername>
                 <fromusername>100012</fromusername>
                 <createtime>{$this->time}</createtime>
                 <msgtype>{$this->event}</msgtype>
                 {$this->judgment()}
                 <msgid>1234567890123456</msgid>
            </xml>
         ";
         return $str;
    }

    //模拟post提交
    private function post(){
        $header[] = "Content-type: text/xml";//定义content-type为xml
        $ch = curl_init(); //初始化curl
        curl_setopt($ch, CURLOPT_URL, $this->url);//设置链接
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//设置是否返回信息
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);//设置HTTP头
        curl_setopt($ch, CURLOPT_POST, 1);//设置为POST方式
        curl_setopt($ch, CURLOPT_POSTFIELDS, $this->xml_data());//POST数据
        $response = curl_exec($ch);//接收返回信息
        if(curl_errno($ch)){//出错则显示错误信息
             print curl_error($ch);
        }
         curl_close($ch); //关闭curl链接
         return $response;
     }

    //文本消息
    private function text(){
        return  "<content>{$this->content}</content>";
    }

    //图形消息
    private function image(){
        return "<picurl>{$this->content}</picurl>";
    }

    //链接消息
    private function link(){
        $data = $this->content;
        $str = "
            <title>{$data[0]}</title>
            <description>{$data[1]}</description>
            <url>{$data[2]}</url>
        ";
        return $str;
    }

    //地理位置消息
    private function location(){
        $data = $this->content;
        $str = "
            <location_x>{$data[0]}</location_x>
            <location_y>{$data[1]}</location_y>
            <scale>20</scale>
            <label>{$data[3]}</label>";
         return $str;
    }
   
    //根据消息类型加载相应的东西
    private function judgment(){
        $type = $this->event;
        return $this->$type();
    }

}

$a = new WinXinPost("text","http://www.php100.com/wx_sample.php",11111);
echo $a->result();

我刚从官网下载的sdk做的测试 wx_sample.php 一点没有改变 前提是你要看wx_sample.php这个东西没有500错误哈,截图如下



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn