注册|登录

联系电话:024-31891684  13390130939
沈阳软件公司--沈阳软件定制

沈阳软件开发_沈阳软件公司_沈阳软件定制/软件/最新技术

Latest technology最新技术

微信平台接口php版

浏览量:6561

微信平台接口php版

<?php

/**

* wechat php test

*/

 

//define your token

define("TOKEN", "yishike1488");

$wechatObj = new wechatCallbackapiTest();

$wechatObj->responseMsg();//主动回复当集成是先调用valid() 集成后改为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();

$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>";

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;

}

}

 

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;

}

}

}

 

?>

沈阳团购网|营口网站制作|沈阳软件公司|软件定制|网站建设|加盟易势|提交问题