搜尋
首頁php教程php手册SpeedPHP微信接口扩展

这个扩展实现了SP和微信公众平台的对接,1.0版暂时只实现了最简单的功能:绑定,收信息,回复信息。 

扩展配置方法: 

$spConfig = array( 
    'mode' => 'debug' 
    //扩展配置 
    , 'ext' => array( 
        //微信扩展设置 
        'spWeiXin' => array( 
            'TOKEN' => 'weixin' //微信通信密钥,后台设置 
        ) 
    ) 
);
 
 然后在controll的方法里面执行: 

$wx = spClass('spWeiXin'); 
        $msg = $wx->run();

    就行了,如果是绑定那么会直接输出微信指定显示的echostr,程序中止执行。如果是收到信息,那么$msg就是收到的信息的数组,键名对照微信官方文档就成。

 

 

/**

 * SpeedPHP微信接口扩展

 *

2013年8月15日 08:51:23

 * @author Lee(leeldy[AT]163.com)

 * @version 1.0

 */

class spWeiXin {

 

    //微信通信密钥

    private $token;

    //微信公众帐号ID

    private $publicuserid;

    //信息接受者

    private $touserid;

 

    public function __construct() {

        $params = spExt('spWeiXin');

        //获取配置参数

        if (empty($params['TOKEN'])) {

            spError('微信公众帐号通信密钥未设置!');

        }

        $this->token = $params['TOKEN'];

    }

 

    /**

     * 绑定微信接口

     * @return stringfalse

     */

    public function bind() {

 

        //随机字符串

        $echoStr = $_GET["echostr"];

        //微信加密签名

        $signature = $_GET["signature"];

        //签名时间戳

        $timestamp = $_GET["timestamp"];

        //加密随机数

        $nonce = $_GET["nonce"];

 

        /*

         * 加密/校验流程:

          1. 将token、timestamp、nonce三个参数进行字典序排序

          2. 将三个参数字符串拼接成一个字符串进行sha1加密

          3. 开发者获得加密后的字符串可与signature对比,标识该请求来源于微信

         */

        $tmpArr = array($this->token, $timestamp, $nonce);

        sort($tmpArr);

        $_signature = sha1(implode($tmpArr));

 

        if ($_signature != $signature) {

            //签名不正确,返回false

            $echoStr = false;

        }

 

        return $echoStr;

    }

 

    /**

     * 接收消息,返回消息数组

     * @return array

     */

    public function receive() {

        $raw = $this->php_fix_raw_query();

        $msg = false;

        if (!empty($raw)) {

            $msg = (array) simplexml_load_string($GLOBALS['HTTP_RAW_POST_DATA'], 'SimpleXMLElement', LIBXML_NOCDATA);

            $this->publicuserid = $msg['ToUserName'];

            $this->touserid = $msg['FromUserName'];

        }

 

        return $msg;

    }

 

    /**

     * 获取 POST 提交的原始数据

     * @author robotreply at gmail dot com (24-Jul-2009 08:17)

     * @return string

     */

    private function php_fix_raw_query() {

 

        // Try globals array

        //试图从全局变量中获取

        if (isset($GLOBALS["HTTP_RAW_POST_DATA"])) {

            return $GLOBALS["HTTP_RAW_POST_DATA"];

        }

        // Try globals variable

        //试图从全局变量中获取

        if (isset($HTTP_RAW_POST_DATA)) {

            return $HTTP_RAW_POST_DATA;

        }

 

        $post = '';

        // Try stream

        //试图从流中获取

        if (!function_exists('file_get_contents')) {

            //php://input is not available with enctype="multipart/form-data".

            $fp = fopen("php://input", "r");

            if ($fp) {

                while (!feof($fp)) {

                    $post = fread($fp, 1024);

                }

 

                fclose($fp);

            }

        } else {

            $post = file_get_contents("php://input");

        }

 

        return $post;

    }

 

    /**

     * 回复文本消息

     *

对于每一个POST请求,开发者在响应包中返回特定xml结构

     *

对该消息进行响应(现支持回复文本、图文、语音、视频、音乐和对收到的消息进行星标操作)

     *

微信服务器在五秒内收不到响应会断掉连接。

     * @param string $Content  回复的消息内容

     * @return stringfalse

     */

    public function replyText($Content) {

        $msg = false;

 

        if (!empty($Content)) {

            //CreateTime 消息创建时间

            $CreateTime = time();

 

            $msg =

    touserid}]]>

    publicuserid}]]>

    {$CreateTime}

   

   

XML;

        }

 

        return $msg;

    }

 

    /**

     * 回复音乐消息

     * @param string $Title 标题

     * @param string $Description 描述

     * @param string $MusicUrl 音乐链接

     * @param string $HQMusicUrl 高质量音乐链接,WIFI环境优先使用该链接播放音乐

     * @return stringfalse

     */

    public function replyMusic($Title, $Description, $MusicUrl, $HQMusicUrl) {

        $msg = false;

 

        if (!empty($MusicUrl)) {

            //CreateTime 消息创建时间

            $CreateTime = time();

 

            $msg =

    touserid}]]>

    publicuserid}]]>

    {$CreateTime}

   

   

       

       

       

       

   

XML;

        }

 

        return $msg;

    }

 

    /**

     * 回复图文消息

     * @param type $Articles 文章列表 array(array(Title,PicUrl,Url))

     * @return stringfalse

     */

    public function replyNews($Articles) {

        $msg = false;

 

        $articlesStr = '';

        //图文消息个数,限制为10条以内

        $ArticleCount = 0;

        foreach ($Articles as $_art) {

            if (!empty($_art['Title']) && !empty($_art['PicUrl']) && !empty($_art['Url'])) {

                $ArticleCount++;

                $articlesStr .= "

   

       

       

       

       

    ";

            }

        }

 

        if (!empty($ArticleCount)) {

            //CreateTime 消息创建时间

            $CreateTime = time();

 

            $msg =

    touserid}]]>

    publicuserid}]]>

    {$CreateTime}

   

    {$ArticleCount}

    {$articlesStr}

   

XML;

        }

 

        return $msg;

    }

 

    /**

     * 运行

     * @return type

     */

    function run() {

        //微信服务器每次请求都会将signature,timestamp,nonce三个参数GET到接口

        //只能通过是否存在echostr来判断是否是接口绑定动作

        if (isset($_GET['echostr'])) {

            //绑定

            exit($this->bind());

        } else {

            //收到信息

            return $this->receive();

        }

    }

 

}

 

?>



陳述
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

Video Face Swap

Video Face Swap

使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

SublimeText3 英文版

SublimeText3 英文版

推薦:為Win版本,支援程式碼提示!

PhpStorm Mac 版本

PhpStorm Mac 版本

最新(2018.2.1 )專業的PHP整合開發工具

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

將Eclipse與SAP NetWeaver應用伺服器整合。

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser是一個安全的瀏覽器環境,安全地進行線上考試。該軟體將任何電腦變成一個安全的工作站。它控制對任何實用工具的訪問,並防止學生使用未經授權的資源。

WebStorm Mac版

WebStorm Mac版

好用的JavaScript開發工具