PHP之微信公眾號驗證token、回複內容、推送訊息的方法

來源:互聯網
上載者:User
本文主要為大家詳細介紹了php代碼實現驗證token、回複圖文\文本、推送訊息的實用類。感興趣的小夥伴們可以參考一下,希望對大家有所協助。

具體代碼如下

<?phpclass Wechat{  private $data = array();  public function __construct($token){    $this -> auth($token, $wxuser) || exit;    if(IS_GET){      echo($_GET['echostr']);      exit;    }else{      $xml = file_get_contents("php://input");        $xml = new SimpleXMLElement($xml);  //file_put_contents('/var/log/test.txt', $xml,FILE_APPEND);      $xml || exit;      foreach ($xml as $key => $value){        $this -> data[$key] = strval($value);      }    }  }  public function request(){    return $this -> data;  }  public function response($content, $type = 'text', $flag = 0){    $this -> data = array('ToUserName' => $this -> data['FromUserName'], 'FromUserName' => $this -> data['ToUserName'], 'CreateTime' => NOW_TIME, 'MsgType' => $type);    $this -> $type($content);    $this -> data['FuncFlag'] = $flag;    $xml = new SimpleXMLElement('<xml></xml>');    $this -> data2xml($xml, $this -> data);    exit($xml -> asXML());  }  private function text($content){    $this -> data['Content'] = $content;  }  private function music($music){    list($music['Title'], $music['Description'], $music['MusicUrl'], $music['HQMusicUrl']) = $music;    $this -> data['Music'] = $music;  }  private function news($news){    $articles = array();    foreach ($news as $key => $value){      list($articles[$key]['Title'], $articles[$key]['Description'], $articles[$key]['PicUrl'], $articles[$key]['Url']) = $value;      if($key >= 9){        break;      }    }    $this -> data['ArticleCount'] = count($articles);    $this -> data['Articles'] = $articles;  }  private function transfer_customer_service($content){    $this -> data['Content'] = '';  }  private function data2xml($xml, $data, $item = 'item'){    foreach ($data as $key => $value){      is_numeric($key) && $key = $item;      if(is_array($value) || is_object($value)){        $child = $xml -> addChild($key);        $this -> data2xml($child, $value, $item);      }else{        if(is_numeric($value)){          $child = $xml -> addChild($key, $value);        }else{          $child = $xml -> addChild($key);          $node = dom_import_simplexml($child);          $node -> appendChild($node -> ownerDocument -> createCDATASection($value));        }      }    }  }  private function auth($token){    $signature = $_GET["signature"];    $timestamp = $_GET["timestamp"];    $nonce = $_GET["nonce"];    $tmpArr = array($token, $timestamp, $nonce);    sort($tmpArr, SORT_STRING);    $tmpStr = implode($tmpArr);    $tmpStr = sha1($tmpStr);    if(trim($tmpStr) == trim($signature)){      return true;    }else{      return false;    }    return true;  }}?>

相關推薦:

TP接入公眾號支付詳解

公眾號開發配置常見錯誤資訊匯總

公眾號實現使用者管理功能

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.