当前位置: 移动技术网 > IT编程>开发语言>PHP > php微信公众号开发(3)php实现简单微信文本通讯

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

2017年12月12日  | 移动技术网IT编程  | 我要评论

微信开发前,需要设置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);

这样就实现了一个例子,第一次关注事件回复,非文本回复,以及文本回复,这里文本回复是你输入什么就返回什么。

具体实现功能就写在文本回复里面。

其他的功能暂时不做,具体开发下节再说。

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网