찾다
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 기반 앱

AI Clothes Remover

AI Clothes Remover

사진에서 옷을 제거하는 온라인 AI 도구입니다.

Undress AI Tool

Undress AI Tool

무료로 이미지를 벗다

Clothoff.io

Clothoff.io

AI 옷 제거제

Video Face Swap

Video Face Swap

완전히 무료인 AI 얼굴 교환 도구를 사용하여 모든 비디오의 얼굴을 쉽게 바꾸세요!

뜨거운 도구

스튜디오 13.0.1 보내기

스튜디오 13.0.1 보내기

강력한 PHP 통합 개발 환경

SublimeText3 중국어 버전

SublimeText3 중국어 버전

중국어 버전, 사용하기 매우 쉽습니다.

드림위버 CS6

드림위버 CS6

시각적 웹 개발 도구

VSCode Windows 64비트 다운로드

VSCode Windows 64비트 다운로드

Microsoft에서 출시한 강력한 무료 IDE 편집기

ZendStudio 13.5.1 맥

ZendStudio 13.5.1 맥

강력한 PHP 통합 개발 환경