人人人妻人人人妻人人人,99精品国产综合久久久久五月天 ,欧美白人最猛性XXXXX,日韩AV无码免费播放

News新聞

業(yè)界新聞動(dòng)態(tài)、技術(shù)前沿
Who are we?

您的位置:首頁(yè)      樂(lè)道系統(tǒng)FAQ      php微信公眾號(hào)開(kāi)發(fā)(3)php實(shí)現(xiàn)簡(jiǎn)單微信文本通訊

php微信公眾號(hào)開(kāi)發(fā)(3)php實(shí)現(xiàn)簡(jiǎn)單微信文本通訊

標(biāo)簽: 發(fā)布日期:2016-12-15 00:00:00 290

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

<?php
class Weixin
{
 public $token = '';//token
 public $debug = false;//是否debug的狀態(tài)標(biāo)示,方便我們?cè)谡{(diào)試的時(shí)候記錄一些中間數(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ā)過(guò)來(lái)的消息(消息內(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']);
 }
 }
//回復(fù)文本消息
 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ù)回復(fù)圖文消息
 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;//微信公眾平臺(tái)圖文回復(fù)的消息一次最多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;
 }
 }
 
}
?>