成人精品一区二区三区中文字幕-成人精品一区二区三区-成人精品一级毛片-成人精品亚洲-日本在线视频一区二区-日本在线视频免费

導航首頁 ? 技術教程 ? php微信公眾號開發(fā)(3)php實現(xiàn)簡單微信文本通訊
全站頭部文字 我要出現(xiàn)在這里
php微信公眾號開發(fā)(3)php實現(xiàn)簡單微信文本通訊 907 2024-01-18   

微信開發(fā)前,需要設置token,這個是微信設置的,可以任意設置,用來實現(xiàn)微信通訊。這里有一個別人寫的微信類,功能還比較不錯。weixin.class.php代碼如下

<?php
class Weixin
{
 public $token = '';//token
 public $debug = false;//是否debug的狀態(tài)標示,方便我們在調(diào)試的時候記錄一些中間數(shù)據(jù)
 public $setFlag = false;
 public $msgtype = 'text'; //('text','image','location')
 public $msg = array();
 
 public function __construct($token,$debug)
 {
 $this->token = $token;
 $this->debug = $debug;
 }
//獲得用戶發(fā)過來的消息(消息內(nèi)容和消息類型 )
 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);
 }
 
//根據(jù)數(shù)組參數(shù)回復圖文消息
 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;
 }
 }
 
}
?>

接著正式開發(fā),使用百度SVN地址,創(chuàng)建weixinapi.php文件,這個根據(jù)你后臺設置名稱。

<?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("暫時沒有圖片,聲音,地理位置等功能,后續(xù)開發(fā)會增加,感謝你關注");
}

$weixin->reply($reply);

這樣就實現(xiàn)了一個例子,第一次關注事件回復,非文本回復,以及文本回復,這里文本回復是你輸入什么就返回什么。

具體實現(xiàn)功能就寫在文本回復里面。

其他的功能暫時不做,具體開發(fā)下節(jié)再說。

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持綠夏網(wǎng)。


主站蜘蛛池模板: 急售二室一厅16万元一层楼| 九九九九九九九伊人| 抖音 下载| 狂野殴美激情性bbbbbb| 圣少女第1集分集剧情| 一级特黄新婚之夜| 电影《七三一》| 刘浩存个人简历资料| 漂流者| 通灵之王| 暗夜尖叫1988美国版高清观看| 散文诗二首批注| 免费播放电影大全免费观看| 2014春节联欢晚会| 南来北往分集剧情| 红星闪闪歌词完整版打印| 成人在线播放网站| 刘越| 电视剧昨夜星辰| 姿metcn张筱雨人体1| 苑琼丹三级| 师奶madam 电视剧| 飞虎神鹰1-42集免费| 徐贤电影| 不要好舒服| 河南电视台卫星频道| 大学生国防论文2000字| 情欲视频| 母亲とが话しています免费| 郭明翔| 乱世三人行| 陈妍希三级露全乳电影| 西街少年 电视剧| angelababy婚礼大作战| 国产高清一卡2卡3卡4卡乱码| 日本电影家庭教师| 服务群众方面整改成效| 同乐赛鸽公棚| 电影不见不散| 赖小子| 西野翔电影|

!!!站長長期在線接!!!

網(wǎng)站、小程序:定制開發(fā)/二次開發(fā)/仿制開發(fā)等

各種疑難雜癥解決/定制接口/定制采集等

站長微信:lxwl520520

站長QQ:1737366103