微信开发实例详解

微信开发,当下如火如荼,似乎是每个程序员必备的技能之一!

本文将详细介绍某微信订阅号的具体代码实例!

备注:微信订阅号菜单,可由微信菜单生成器自动生成!

代码讲解:

<?php
/*
PHPthinking
www.phpthinking.com
*/

define("TOKEN", "cok123");//本微信号TOKEN

$wechatObj = new wechatCallbackapiTest();
if (!isset($_GET['echostr'])) {
$wechatObj->responseMsg();
}else{
$wechatObj->valid();
}

class wechatCallbackapiTest
{
//验证签名
public function valid()
{
$echoStr = $_GET["echostr"];
$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){
echo $echoStr;
exit;
}
}

public function responseMsg()
{
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
if (!empty($postStr)){
$this->logger("R ".$postStr);
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$RX_TYPE = trim($postObj->MsgType);

$result = "";
switch ($RX_TYPE)
{
case "event"://事件类型调用
$result = $this->receiveEvent($postObj);
break;
case "text"://文字回复调用
$result = $this->receiveText($postObj);
break;
}
$this->logger("T ".$result);
echo $result;
}else {
echo "";
exit;
}
}

private function receiveEvent($object)
{
switch ($object->Event)
{
case "subscribe"://关注后,推送的消息
$content = "感谢您的关注!";
break;
case "CLICK"://点击菜单按钮触发事件
$url = "http://www.phpthinking.com/sign.php?&openid=".$object->FromUserName; //用户获取该链接的信息,openid标识用户的唯一性,试用于签到领奖等功能
$output = file_get_contents($url);//得到json返回信息
$output = json_decode($output, true);//转化为数组
if($output[1]==1){//例如,签到功能
$title = "您本周已签到!";
$desc = "点击图片查看";
}else{
$title = "恭喜您签到成功!";
$desc = "点击图片领取";
}
$content = array();
$content[] = array("Title" =>$title,
"Description" =>$desc,
"PicUrl" =>"https://www.phpthinking.com?wx_fmt=jpeg",//微信图文消息中的图片链接
"Url" =>"http://www.phpthinking.com/index.php?sign=".$output[0]);//点击图文消息后的跳转链接,同时传递参数

return $result = $this->transmitNews($object, $content);//调用方法,推送消息
break;
}
$result = $this->transmitText($object, $content);//调用方法,推送消息
return $result;
}

private function receiveText($object)
{
$keyword = trim($object->Content);

if($keyword=='官网'||$keyword=='网站'){
$content = 'www.phpthinking.com';//当用户回复官网或网站时,自动回复此内容
return $result = $this->transmitText($object, $content);//推送消息
}
if(strstr($keyword, '签到') || strstr($keyword, '礼包')){//当用户回复含签到或礼包的关键字时,自动回复此内容
$content = array();
$content[] = array("Title" =>'签到礼包暂时下架说明',
"Description" =>'尊敬的玩家:你们好!我们将在今日18:00关闭"签到礼包"的领取,请各位知晓。',
"PicUrl" =>"https://www.phpthinking.com?wx_fmt=jpeg",
"Url" =>"http://www.phpthinking.com");
return $result = $this->transmitNews($object, $content);//推送消息
}

return '';
}

private function transmitText($object, $content)//推送文字消息方法
{
if (!isset($content) || empty($content)){
return "";
}
$textTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
$result = sprintf($textTpl, $object->FromUserName, $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 logger($log_content)
{

}
}
?>

打赏此文

如果您觉得本站的内容对您有所帮助,您可以扫描下面的二维码小额支付请我喝杯茶,感谢!打赏记录
支付宝
微信
承诺:凡打赏捐助的朋友,留言备注自己的邮箱,在打赏捐助时间点的6个月内,本站会每周邮件推送原创专业技术博文,供大家学习和参考!

留下评论

All fields marked (*) are required