泰山神 发表于 2015-11-17 13:11:31

php开发微信demo

  已经没有搞php一年多了,除了几个简单的输出和方法外,其他的我都忘得差不多了!
  


  今天贴出代码,供给那些懂的人去研究!
  


  <?php
/**
* wechat php test
*/
//define your token
define(&quot;TOKEN&quot;, &quot;weixin&quot;);
$wechatObj = new wechatCallbackapiTest();
$wechatObj->valid();
class wechatCallbackapiTest
{
public function valid()
{
$echoStr = $_GET[&quot;echostr&quot;];
//valid signature , option
if($this->checkSignature()){
echo $echoStr;
exit;
}
}
public function responseMsg()
{
//get post data, May be due to the different environments
$postStr = $GLOBALS[&quot;HTTP_RAW_POST_DATA&quot;];
//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 = &quot;<xml>
<ToUserName><!]></ToUserName>
<FromUserName><!]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><!]></MsgType>
<Content><!]></Content>
<FuncFlag>0</FuncFlag>
</xml>&quot;;            
if(!empty( $keyword ))
{
$msgType = &quot;text&quot;;
$contentStr = &quot;Welcome to wechat world!&quot;;
$resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr);
echo $resultStr;
}else{
echo &quot;Input something...&quot;;
}
}else {
echo &quot;&quot;;
exit;
}
}
private function checkSignature()
{
$signature = $_GET[&quot;signature&quot;];
$timestamp = $_GET[&quot;timestamp&quot;];
$nonce = $_GET[&quot;nonce&quot;];
$token = TOKEN;
$tmpArr = array($token, $timestamp, $nonce);
sort($tmpArr);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
}
?>





版权声明:本文为博主原创文章,未经博主允许不得转载。
页: [1]
查看完整版本: php开发微信demo