国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

php微信公眾號開發(3)php實現簡單微信文本通訊

2024-05-04 23:53:44
字體:
來源:轉載
供稿:網友

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

<?phpclass 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文件,這個根據你后臺設置名稱。

<?phpdefine("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);

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

具體實現功能就寫在文本回復里面。

其他的功能暫時不做,具體開發下節再說。

以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持VeVb武林網。


注:相關教程知識閱讀請移步到PHP教程頻道。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 沛县| 七台河市| 慈利县| 乌什县| 绵竹市| 青田县| 芦山县| 宁波市| 汉阴县| 洛扎县| 古田县| 崇文区| 台东县| 新绛县| 临城县| 扬中市| 南城县| 炉霍县| 汉寿县| 会昌县| 福州市| 忻州市| 墨玉县| 房产| 鄂伦春自治旗| 塘沽区| 涞源县| 花莲市| 镇宁| 广宁县| 正蓝旗| 浮山县| 车致| 农安县| 红桥区| 丹寨县| 崇礼县| SHOW| 水富县| 修文县| 福贡县|