php微信公众号开发(3)php实现简单微信文本通讯

黄舟
发布: 2018-05-15 16:28:56
原创
2725人浏览过

微信开发前,需要设置token,这个是微信设置的,可以任意设置,用来实现微信通讯。这里有一个别人写的微信类,功能还比较不错。weixin.class.php代码如下

<?php
class Weixin
{
 public $token = '';//token
 public $debug = false;//是否debug的状态标示,方便我们在调试的时候记录一些中间数据
 public $setFlag = false;
 public $msgtype = 'text'; //('text','image','location')
 public $msg = array();
 
 public function __construct($token,$debug)
 {
 $this->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 = "<xml>
  <ToUserName><![CDATA[{$this->msg['FromUserName']}]]></ToUserName>
  <FromUserName><![CDATA[{$this->msg['ToUserName']}]]></FromUserName>
  <CreateTime>{$CreateTime}</CreateTime>
  <MsgType><![CDATA[text]]></MsgType>
  <Content><![CDATA[%s]]></Content>
  <FuncFlag>%s</FuncFlag>
  </xml>";
 return sprintf($textTpl,$text,$FuncFlag);
 }
 
//根据数组参数回复图文消息
 public function makeNews($newsData=array())
 {
 $CreateTime = time();
 $FuncFlag = $this->setFlag ? 1 : 0;
 $newTplHeader = "<xml>
  <ToUserName><![CDATA[{$this->msg['FromUserName']}]]></ToUserName>
  <FromUserName><![CDATA[{$this->msg['ToUserName']}]]></FromUserName>
  <CreateTime>{$CreateTime}</CreateTime>
  <MsgType><![CDATA[news]]></MsgType>
  <Content><![CDATA[%s]]></Content>
  <ArticleCount>%s</ArticleCount><Articles>";
 $newTplItem = "<item>
  <Title><![CDATA[%s]]></Title>
  <Description><![CDATA[%s]]></Description>
  <PicUrl><![CDATA[%s]]></PicUrl>
  <Url><![CDATA[%s]]></Url>
  </item>";
 $newTplFoot = "</Articles>
  <FuncFlag>%s</FuncFlag>
  </xml>";
 $Content = '';
 $itemsCount = count($newsData['items']);
 $itemsCount = $itemsCount < 10 ? $itemsCount : 10;//微信公众平台图文回复的消息一次最多10条
 if ($itemsCount) {
  foreach ($newsData['items'] as $key => $item) {
  if ($key<=9) {
   $Content .= sprintf($newTplItem,$item['title'],$item['description'],$item['picurl'],$item['url']);
  }
  }
 }
 $header = sprintf($newTplHeader,$newsData['content'],$itemsCount);
 $footer = sprintf($newTplFoot,$FuncFlag);
 return $header . $Content . $footer;
 }
 public function reply($data)
 {
 
 echo $data;
 }
 public function valid()
 {
 if ($this->checkSignature()) {
  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;
 }
 }
 
}
?>
登录后复制

接着正式开发,使用百度SVN地址,创建weixinapi.php文件,这个根据你后台设置名称。

<?php
define("TOKEN", "");
define('DEBUG', false);
include_once('weixin.class.php');
require_once("db.php");
  
$weixin = new Weixin(TOKEN,DEBUG);//实例化
$weixin->getMsg();
$type = $weixin->msgtype;//消息类型
$keyword = $weixin->msg['Content'];//获取的文本
if ($type==='text') {
$reply = $weixin->makeText($key);
}elseif($type==='event'){//第一次关注推送事件
 $reply = $weixin->makeText("欢迎关注");
}else{//其他类型
$reply = $weixin->makeText("暂时没有图片,声音,地理位置等功能,后续开发会增加,感谢你关注");
}

$weixin->reply($reply);
登录后复制

这样就实现了一个例子,第一次关注事件回复,非文本回复,以及文本回复,这里文本回复是你输入什么就返回什么。
具体实现功能就写在文本回复里面。
其他的功能暂时不做,具体开发下节再说。

以上就是php微信公众号开发(3)php实现简单微信文本通讯的内容,更多相关内容请关注PHP中文网(www.php.cn)!

微信app下载
微信app下载

微信是一款手机通信软件,支持通过手机网络发送语音短信、视频、图片和文字。微信可以单聊及群聊,还能根据地理位置找到附近的人,带给大家全新的移动沟通体验,有需要的小伙伴快来保存下载体验吧!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号