PHP对接微信公众号,并实现返回给用户文字或图片或图文的内容。

来源:互联网 发布:淘宝 prd mrd 模板 编辑:程序博客网 时间:2024/06/15 03:04

代码示例:

<?phpdefine("TOKEN", "weixin");//我用的token名是weixin,你可以自己换$wechatObj = new wechatCallbackapiTest();$wechatObj->responseMsg();class wechatCallbackapiTest{public function valid(){        $echoStr = $_GET["echostr"];        if($this->checkSignature()){        echo $echoStr;        exit;        }    }    public function responseMsg(){$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];if (!empty($postStr)){                libxml_disable_entity_loader(true);              $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);                $fromUsername = $postObj->FromUserName;                $toUsername = $postObj->ToUserName;                $keyword = trim($postObj->Content);$event = $postObj->Event;                $time = time();                $textTpl = "<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[%s]]></MsgType><Content><![CDATA[%s]]></Content><FuncFlag>0</FuncFlag></xml>";  switch($postObj->MsgType){case 'event':if($event == 'subscribe'){//关注后的回复$contentStr = "关注后的回复";$msgType = 'text';$textTpl = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);echo $textTpl;}break;case 'text':$newsTpl = "<xml>                <ToUserName><![CDATA[%s]]></ToUserName>                <FromUserName><![CDATA[%s]]></FromUserName>                <CreateTime>%s</CreateTime>                <MsgType><![CDATA[%s]]></MsgType>                <Image>                <MediaId><![CDATA[%s]]></MediaId>                </Image>                <FuncFlag>0</FuncFlag>                </xml>";if($keyword=="图片"){$msgType = 'image';$contentStr = "GlCd0HTzI8A1MzDZIkAlcLkXj68YRyRRbpunVx00Hc0";//这里的contentstr填写的是图片的media_id,再你公众号后台的素材管理里,有图片素材,就是那些图片素材的id,至于如何获取,请看我的另一篇博客。$resultStr = sprintf($newsTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);//这里的newstpl,我定义的是image类型,名字随便起,如果你想返回文字类型,就用texttpl。echo $resultStr; }else if($keyword=="图文"){$tuwenTpl = "<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[news]]></MsgType><ArticleCount>1</ArticleCount><Articles><item><Title><![CDATA[%s]]></Title> <Description><![CDATA[%s]]></Description><PicUrl><![CDATA[%s]]></PicUrl><Url><![CDATA[%s]]></Url></item></Articles></xml>";$title = '不好意思,没找到哟,请留言!';//这是标题$des1 ="";//这是描述$picUrl1 ="http://www.nichousha.cc/2.jpg";//这是引用的图片地址$url="http://nichousha.cc/wap/index.php?m=gbook-show.html";//这是点击图文后跳转的地址$resultStr= sprintf($tuwenTpl, $fromUsername, $toUsername, $time, $title, $des1, $picUrl1, $url) ;echo $resultStr; }$contentStr = "你说啥,有种用普通话再说一遍!";//如果用户输入的信息,都没有匹配成功,就返回这一句$msgType = 'text';$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);echo $resultStr;break;default:break;}        }else {        echo "你好!欢迎进入我的微信公众号";        exit;        }    }private function checkSignature(){        // you must define TOKEN by yourself        if (!defined("TOKEN")) {            throw new Exception('TOKEN is not defined!');        }                $signature = $_GET["signature"];        $timestamp = $_GET["timestamp"];        $nonce = $_GET["nonce"];        $token = TOKEN;$tmpArr = array($token, $timestamp, $nonce);        // use SORT_STRING rulesort($tmpArr, SORT_STRING);$tmpStr = implode( $tmpArr );$tmpStr = sha1( $tmpStr );if( $tmpStr == $signature ){return true;}else{return false;}}}?>
将这个文件上传到你的服务器上,然后在微信公众号后台找到基础配置,把服务器地址(这个文件在你服务器上的路径)和令牌(我用的是weixin)填写进去就ok了。

阅读全文
0 0