微信公众号开发

<?php
define("TOKEN", "******");
$wechatObj = new wechatCallbackapiTest();
//$wechatObj->valid();
//$wechatObj->respond();

class wechatCallbackapiTest
{
public function valid()
{
$echoStr = $_GET["echostr"];

//valid signature , option
if($this->checkSignature()){
echo $echoStr;
exit;
}
}

private function checkSignature()
{
// you must define TOKEN by yourself
if (!defined("TOKEN")) {
throw new Exception(‘TOKEN is not defined!‘);
}

$signature = $_GET["signature"];
$timestamp = $_GET["timestamp"];
$nonce = $_GET["nonce"];

$token = TOKEN;
$tmpArr = array($token, $timestamp, $nonce);
// use SORT_STRING rule
sort($tmpArr, SORT_STRING);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );

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

}



//下面是微信的xml信息

$source = $GLOBALS[‘HTTP_RAW_POST_DATA‘];

$source1 = "<xml>
<ToUserName><![CDATA[gh_2eebd1c1d92e]]></ToUserName>
<FromUserName><![CDATA[o8KDujlmvJYFxNe26jmtquaX05Rg]]></FromUserName>
<CreateTime>1356076016</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[岗顶]]></Content>
</xml>";

$params = !empty($source) && is_string($source) ? @simplexml_load_string($source, ‘SimpleXMLElement‘, LIBXML_NOCDATA) : null;

$FromUserName = trim($params->FromUserName);

switch (trim($params->MsgType)) {
case ‘text‘:
$content = trim($params->Content);
if ($content) {
output($FromUserName, $content, true);
}
break;

case ‘event‘:
if ($params->Event == "SCAN") {//扫码
if (preg_match("#^1$#", $params->EventKey)) {//传入了用户数字id
// $content = trim($params->EventKey);
//$result1 = array();
//$result2 = array();
//file_put_contents("txt.log",var_export($params,true),FILE_APPEND);
//$content = trim($params->EventKey);
$content="恭喜您获得优惠券!戳链接领取<a href=‘http://url.cn/48Ylmju‘>百度外卖新手大礼包</a>";
output($FromUserName, $content, true);
}
if (preg_match("#^2$#", $params->EventKey)) {//传入了用户数字id
// $content = trim($params->EventKey);
//$result1 = array();
//$result2 = array();
//file_put_contents("txt.log",var_export($params,true),FILE_APPEND);
//$content = trim($params->EventKey);
$content="恭喜您获得优惠券!戳链接领取<a href=‘https://dorado.xiaojukeji.com/share.html?a=9717&b=24102&c=24100&s=148488051956077276&m=MTg1ODg2NTYzODU=&ts=1484880519&fc=d00ed4f8e3fd1546d661ea3dcf9513c6&t=QR&d=0‘>滴滴出行打车券</a>";
output($FromUserName, $content, true);
}
}

if ($params->Event == "subscribe") {
$content="欢迎您使用天天拼吧!超值红包大礼奉上
1.送你280元投资红包赚大钱,<a href=‘https://lantouzi.com/union/hello?pcode=hi86xq&hmsr=hi86xq&hmmd=cpc&hmpl=zonghe‘>马上领取>></a>
2.美团外卖15元超值红包,<a href=‘http://url.cn/48YrgMy‘>马上领取>></a>
3.蘑菇街送你20元现金礼包,<a href=‘http://url.cn/48Yp71Y‘>马上领取>></a>
4.唯品会专享30元红包,<a href=‘http://url.cn/48YmcyL‘>马上领取>></a>
------------------
天天拼吧给您提供各类红包优惠券,包括滴滴,百度,美团,饿了么,唯品会,蘑菇街,懒投资等等,推送超高性价比的商品信息,定时分享最省钱的网购攻略。关注天天拼吧,我们为你精选最优商品,在网购路上做个精明小能手!";
output($FromUserName, $content, true);
}

break;

default :
break;

}




function output($ToUserName, $Content, $short = false){
$output = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[gh_4fdbdd0ce2ca]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[%s]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";

$Content = sprintf($output, $ToUserName, time(), "text", $Content);
//$Content = trim($Content);
echo $Content;
exit();
}


微信公众号开发

上一篇:MyBatis(2)——CRUD操作


下一篇:H5调起微信支付功能