欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

微信开发 多图图文信息PHP代码

程序员文章站 2024-01-19 13:59:28
...
<?php
/**
  * wechat php test
  */

//define your token
define("TOKEN", "weixin");
$wechatObj = new wechatCallbackapiTest();
//$wechatObj->valid();
$wechatObj->responseMsg();
class wechatCallbackapiTest
{
    public function valid()
    {
        $echoStr = $_GET["echostr"];

        //valid signature , option
        if($this->checkSignature()){
            echo $echoStr;
            exit;
        }
    }

    public function responseMsg()
    {
        //get post data, May be due to the different environments
        $postStr = $GLOBALS["HTTP_RAW_POST_DATA"];

        //extract post data
        if (!empty($postStr)){

                $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
                $fromUsername = $postObj->FromUserName;
                $toUsername = $postObj->ToUserName;
                $keyword = trim($postObj->Content);
                $time = time();
                $newsTpl = "<xml>
                            <ToUserName><![CDATA[%s]]></ToUserName>
                            <FromUserName><![CDATA[%s]]></FromUserName>
                            <CreateTime>%s</CreateTime>
                            <MsgType><![CDATA[news]]></MsgType>
                            <ArticleCount>2</ArticleCount>
                             <Articles>
                             <item>
                             <Title><![CDATA[保险法司法解释二出台]]></Title> 
                             <Description><![CDATA[对保险公司更加有利]]></Description>
                             <PicUrl><![CDATA[http://weixin1234.duapp.com/1.jpg]]></PicUrl>
                             <Url><![CDATA[http://www.chinacourt.org/article/detail/2013/06/id/1014893.shtml]]></Url>
                             </item>
                             <item>
                             <Title><![CDATA[保险法全文]]></Title> 
                             <Description><![CDATA[1]]></Description>
                             <PicUrl><![CDATA[http://t1.baidu.com/it/u=3340518375,2607788323&fm=23&gp=0.jpg]]></PicUrl>
                             <Url><![CDATA[http://www.gov.cn/flfg/2009-02/28/content_1246444.htm]]></Url>
                             </item>
                            <FuncFlag>0</FuncFlag>
                            </xml>";             
                if(!empty( $keyword ))
                {

                    $resultStr = sprintf($newsTpl, $fromUsername, $toUsername, $time);
                    echo $resultStr;
                }else{
                    echo "Input something...";
                }

        }else {
            echo "";
            exit;
        }
    }

    private function checkSignature()
    {
        $signature = $_GET["signature"];
        $timestamp = $_GET["timestamp"];
        $nonce = $_GET["nonce"];    

        $token = TOKEN;
        $tmpArr = array($token, $timestamp, $nonce);
        sort($tmpArr);
        $tmpStr = implode( $tmpArr );
        $tmpStr = sha1( $tmpStr );

        if( $tmpStr == $signature ){
            return true;
        }else{
            return false;
        }
    }
}

?>
相关标签: 多图图文信息