|
- <?php
- defined('BASEPATH') OR exit('No direct script access allowed');
- class Index extends CI_Controller {
- public function __construct(){
- parent::__construct();
- }
- /*微信接入的验证*/
- public function index()
- {
- $signature = $this->input->get('signature');
- $nonce = $this->input->get('nonce');
- $timestamp = $this->input->get('timestamp');
- //第一次验证才会有,其他时候用户发送时候没有
- $echostr = $this->input->get('echostr');
- $token = "MYTOKEN";
- $arr = array($nonce,$timestamp,$token);
- sort($arr);
- $str = implode('', $arr);
- $str = sha1($str);
- if($str == $signature && $echostr){
- //如果第一次接入时候
- echo $echostr;
- exit;
- }else{
- //用户访问时候也要验证,但是没有echostr
- $this->responseMsg();
- }
- }
- /*微信接受消息回复*/
- public function responseMsg(){
- //用于得到php不能识别的post的数据
- $postArr = isset($GLOBALS['HTTP_RAW_POST_DATA']) ? $GLOBALS['HTTP_RAW_POST_DATA'] : file_get_contents("php://input");
-
- $postObj = simplexml_load_string($postArr,'SimpleXMLElement', LIBXML_NOCDATA);
-
- if($postObj){
- $MsgType = $postObj->MsgType;
- if($MsgType=='text' ){
- $template = "<xml>
- <ToUserName><![CDATA[%s]]></ToUserName>
- <FromUserName><![CDATA[%s]]></FromUserName>
- <CreateTime>%s</CreateTime>
- <MsgType><![CDATA[%s]]></MsgType>
- <Content><![CDATA[%s]]></Content>
- </xml>";
- $toUser = $postObj->FromUserName;
- $fromUser = $postObj->ToUserName;
- $createTime = time();
- $msgType = 'text';
- $content = "欢迎您来见证我和仙女的爱情!";
- $backInfo = sprintf($template,$toUser,$fromUser,$createTime,$msgType,$content);
- echo $backInfo;
- }
- }
- else{
- echo '还未获到参数';
- }
- }
- }
复制代码
是什么地方出了错误啊?还不好调试啊?! |
|