xgnic 发表于 2018-2-11 12:00:09


            微信开发前,需要设置token,这个是微信设置的,可以任意设置,用来实现微信通讯。这里有一个别人写的微信类,功能还比较不错。weixin.class.php代码如下
token = $token;
$this->debug = $debug;
}
//获得用户发过来的消息(消息内容和消息类型 )
public function getMsg()
{
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];

if (!empty($postStr)) {
$this->msg = (array)simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$this->msgtype = strtolower($this->msg['MsgType']);
}
}
//回复文本消息
public function makeText($text='')
{
$CreateTime = time();
$FuncFlag = $this->setFlag ? 1 : 0;
$textTpl = "
msg['FromUserName']}]]>
msg['ToUserName']}]]>
{$CreateTime}


%s
";
return sprintf($textTpl,$text,$FuncFlag);
}

//根据数组参数回复图文消息
public function makeNews($newsData=array())
{
$CreateTime = time();
$FuncFlag = $this->setFlag ? 1 : 0;
$newTplHeader = "
msg['FromUserName']}]]>
msg['ToUserName']}]]>
{$CreateTime}


%s";
$newTplItem = "




";
$newTplFoot = "
%s
";
$Content = '';
$itemsCount = count($newsData['items']);
$itemsCount = $itemsCount$item) {
if ($keycheckSignature()) {
if( $_SERVER['REQUEST_METHOD']=='GET' )
{
echo $_GET['echostr'];
exit;
}
}else{

exit;
}
}
private function checkSignature()
{
$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];

$tmpArr = array($this->token, $timestamp, $nonce);
sort($tmpArr);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );

if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}

}
?>
页: [1]
查看完整版本: php微信公众号开发(3)php实现简单微信文本通讯