Home  >  Article  >  Backend Development  >  微信公众平台,php开发的简单问题

微信公众平台,php开发的简单问题

WBOY
WBOYOriginal
2016-06-23 13:48:25778browse

我注册了一个微信公众平台的测试账号,并且把url和token都做了设置
url:***.***.com/weixin/weinxin.php
token:weixin
这个url是真实存在且外网可以访问的,weixin.php我用的是腾讯官方提供的

<?php/**  * wechat php test  *///define your tokendefine("TOKEN", "weixin");$wechatObj = new wechatCallbackapiTest();$wechatObj->valid();class wechatCallbackapiTest{	public function valid()    {        $echoStr = $_GET["echostr"];        //valid signature , option        if($this->checkSignature()){        	echo $echoStr;        	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;		}	}}?>

我用自己的微信号关注这个测试微信号,完后我用自己的微信号发消息给这个测试账号,但是并没有收到测试账号回复我的消息,照理说不是应该回复给我相同的消息的吗?有谁弄过这个,什么原因啊


回复讨论(解决方案)

这个是验证token的,如果要回复的话,$wechatObj->valid();要注释掉,换成$wechatObj->responseMsg()
这个函数是微信官方demo里的

 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();
                $textTpl = "


%s


0
";             
if(!empty( $keyword )) {
               $msgType = "text";
                 $contentStr = "Welcome to wechat world!";
                 $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
                 echo $resultStr;
                }else{
                 echo "Input something...";
                }

        }else {
         echo "";
         exit;
        }
    }

upup! 不错

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn