需求:
已测试号为例;用户发起拼团活动(为团长),服务器发送他带有活动id与其openid的二维码图片,转发给他人,已关注的扫码之后保存其openid,未关注的在关注之后保存openid(防止重复参与)(团员),然后自动回复团员自己活动信息的二维码。
1.微信公众号后台配置好 服务器回应地址
2.服务器回应:数据暂时放在redis中。
包含关注、取消关注事件,发送文字、图片、音频事件。
<?php
include_once('/op********he/memcache.php');
include('tuan/token.php'); //获取token
include('tuan/get_prcode.php'); //获得二维码(带有活动id和发起人openid)
include('tuan/put_tongzhi.php');//有人帮助发送模板消息(发起人与帮助者)
include('tuan/tuan_redis.php'); //保存成团信息到redis
define("TOKEN", "dianjixia");
$wechatObj = new wechatCallbackapi();
if($_GET["echostr"])//验证签名
{
$wechatObj->valid();
}else//其他事件
{
$wechatObj->responseMsg();
}
class wechatCallbackapi
{
public function valid()
{
$echoStr = $_GET["echostr"];
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,SORT_STRING);
$tmpStr = implode( $tmpArr );
$tmpStr = sha1( $tmpStr );
if( $tmpStr == $signature ){
return true;
}else{
return false;
}
}
public function responseMsg()
{
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
if (!empty($postStr))
{
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$RX_TYPE = trim($postObj->MsgType);
switch($RX_TYPE)
{
case "text":
$resultStr = $this->handleText($postObj);
break;
case "voice":
$resultStr = $this->receiveVoice($postObj);
break;
case "image":
$resultStr = $this->receiveImg($postObj);
break;
case "event":
$resultStr = $this->handleEvent($postObj);
break;
default:
$resultStr = "Unknow msg type: ".$RX_TYPE;
break;
}
echo $resultStr;
}else
{
echo "error";
exit;
}
}
//处理用户文字提问
public function handleText($postObj)
{
$fromUsername = $postObj->FromUserName;
$toUsername = $postObj->ToUserName;
$content = trim($postObj->Content);
if($content == '拼团'){ //测试拼团触发事件(暂时为文字)
$act_id = '906';
$mediaID = get_prcode($postObj->FromUserName,$act_id);
if($mediaID){
$resultStr = $this->transmitPic($postObj,$mediaID);
return $resultStr;
}else{
$resultStr = $this->transmitText($postObj,'未获取到mediaID'.$postObj->FromUserName);
return $resultStr;
}
}
public function handleEvent($object)
{
$contentStr = "";
$qrcode = json_decode(json_encode($object),true);
switch ($object->Event)
{
case "SCAN":
if(count($qrcode['EventKey']) == 1 ){
$my_openid = $qrcode['FromUserName'];
$code_arr = explode(",",$qrcode['EventKey']);
$p_openid = $code_arr[0];
$act_id = $code_arr[1];
//$resultStr = $this->transmitText($object,$p_openid.','.$act_id.','.$my_openid);
//return $resultStr;
$state = joinTuan($p_openid,$act_id,$my_openid);
if($state == 2){
$resultStr = $this->transmitText($object, '此团已满');
}elseif($state == 3){
$resultStr = $this->transmitText($object, '您已进过此团');
}elseif($state == 4){
//header("Location: http://www.qubaobei.com/weekend/view/mall_mm/pages/rule.php?$my_openid");
$resultStr = $this->transmitText($object, '暂时先给提示语;自己的团a');
}else{
put_tongzhi($p_openid,$act_id); //发送通知给发起人
put_my_tongzhi($my_openid); //发送通知给扫码人
$mediaID = get_prcode($object->FromUserName,$act_id);
$resultStr = $this->transmitPic($object,$mediaID);
//$resultStr = $this->transmitText($object,$state.','.$mediaID);
}
return $resultStr;
}
case "subscribe":
if(count($qrcode['EventKey']) == 1){
$my_openid = $qrcode['FromUserName'];
$code_arr = explode(",",$qrcode['EventKey']);
$p_openid = str_replace('qrscene_','',$code_arr[0]);
$act_id = $code_arr[1];
//$resultStr = $this->transmitText($object,$p_openid.','.$act_id.','.$my_openid);
//return $resultStr;
$state = joinTuan($p_openid,$act_id,$my_openid);
if($state == 2){
$resultStr = $this->transmitText($object, '此团已满');
}elseif($state == 3){
$resultStr = $this->transmitText($object, '您已进过此团');
}elseif($state == 4){
//header("Location: http://www.qubaobei.com/weekend/view/mall_mm/pages/rule.php?$my_openid");
$resultStr = $this->transmitText($object, '暂时先给提示语;自己的团');
}else{
put_tongzhi($p_openid); //发送通知给发起人
put_my_tongzhi($my_openid); //发送通知给扫码人
$mediaID = get_prcode($object->FromUserName,$act_id);
$resultStr = $this->transmitPic($object,$mediaID);
//$resultStr = $this->transmitText($object,$p_openid);
}
return $resultStr;
}
case "unsubscribe"://取消关注
break;
case "CLICK":
break;
default :
$contentStr = "Unknow Event: ".$object->Event;
break;
}
}
//回复文本消息
private function transmitText($object, $content)
{
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[%s]]></Content>
<FuncFlag>0</FuncFlag>
</xml>";
$result = sprintf($textTpl, $object->FromUserName, $object->ToUserName, time(), $content);
//$result = sprintf($textTpl,'oBxp20nUq4tcxQONMKIupYRxiv04', $object->ToUserName, time(), $content);
return $result;
}
//回复图文消息
private function transmitNews($object, $newsArray)
{
if(!is_array($newsArray)){
return;
}
$itemTpl = "<item>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
<PicUrl><![CDATA[%s]]></PicUrl>
<Url><![CDATA[%s]]></Url>
</item>
";
$item_str = "";
foreach ($newsArray as $item){
$item_str .= sprintf($itemTpl, $item['title'], $item['description'], $item['picurl'], $item['url']);
}
$newsTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<Content><![CDATA[]]></Content>
<ArticleCount>%s</ArticleCount>
<Articles>
$item_str</Articles>
</xml>";
$result = sprintf($newsTpl, $object->FromUserName, $object->ToUserName, time(), count($newsArray));
return $result;
}
//回复图片消息
private function transmitPic($object,$media)
{
/*
$textTpl = "<xml>
<ToUserName>< ![CDATA[%s]]></ToUserName>
<FromUserName>< ![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType>< ![CDATA[image]]></MsgType>
<Image>
<MediaId>< ![CDATA[%s]]></MediaId>
</Image>
</xml>";
*/
$textTpl = "<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[image]]></MsgType><Image><MediaId><![CDATA[%s]]></MediaId></Image></xml>";
$result = sprintf($textTpl,$object->FromUserName, $object->ToUserName, time(),$media);
return $result;
}
//处理图片
private function receiveImg($object)
{
}
}
function curl_baby($url,$data=null)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0)');
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_AUTOREFERER, 1);
//curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$tmpInfo = curl_exec($ch);
if (curl_errno($ch)) {
return curl_error($ch);
}
curl_close($ch);
return $tmpInfo;
}
?>