温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

php如何实现简单微信文本通讯

发布时间:2021-01-28 15:08:55 阅读:129 作者:小新 栏目:移动开发
PHP开发者专用服务器限时活动,0元免费领,库存有限,领完即止! 点击查看>>

这篇文章将为大家详细讲解有关php如何实现简单微信文本通讯,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

微信开发前,需要设置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如何实现简单微信文本通讯”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,使各位可以学到更多知识,如果觉得文章不错,请把它分享出去让更多的人看到。

亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php
AI

开发者交流群×