PHP WeChat 공개 계정 인증 토큰, 답변 내용, 푸시 메시지 방식

*文
풀어 주다: 2023-03-18 20:00:02
원래의
3322명이 탐색했습니다.

이 글에서는 PHP 코드를 구현하여 토큰 확인, 그래픽 텍스트 응답, 푸시 메시지 등을 구현하는 실용적인 WeChat 클래스를 중심으로 자세히 소개합니다. 관심 있는 친구들이 참고하면 좋을 것 같고, 모두에게 도움이 되길 바랍니다.

구체적인 코드는 다음과 같습니다

<?php
class Wechat{
  private $data = array();
  public function __construct($token){
    $this -> auth($token, $wxuser) || exit;
    if(IS_GET){
      echo($_GET[&#39;echostr&#39;]);
      exit;
    }else{
      $xml = file_get_contents("php://input");
  
      $xml = new SimpleXMLElement($xml);
  //file_put_contents(&#39;/var/log/test.txt&#39;, $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 = &#39;text&#39;, $flag = 0){
    $this -> data = array(&#39;ToUserName&#39; => $this -> data[&#39;FromUserName&#39;], &#39;FromUserName&#39; => $this -> data[&#39;ToUserName&#39;], &#39;CreateTime&#39; => NOW_TIME, &#39;MsgType&#39; => $type);
    $this -> $type($content);
    $this -> data[&#39;FuncFlag&#39;] = $flag;
    $xml = new SimpleXMLElement(&#39;<xml></xml>&#39;);
    $this -> data2xml($xml, $this -> data);
    exit($xml -> asXML());
  }
  private function text($content){
    $this -> data[&#39;Content&#39;] = $content;
  }
  private function music($music){
    list($music[&#39;Title&#39;], $music[&#39;Description&#39;], $music[&#39;MusicUrl&#39;], $music[&#39;HQMusicUrl&#39;]) = $music;
    $this -> data[&#39;Music&#39;] = $music;
  }
  private function news($news){
    $articles = array();
    foreach ($news as $key => $value){
      list($articles[$key][&#39;Title&#39;], $articles[$key][&#39;Description&#39;], $articles[$key][&#39;PicUrl&#39;], $articles[$key][&#39;Url&#39;]) = $value;
      if($key >= 9){
        break;
      }
    }
    $this -> data[&#39;ArticleCount&#39;] = count($articles);
    $this -> data[&#39;Articles&#39;] = $articles;
  }
  private function transfer_customer_service($content){
    $this -> data[&#39;Content&#39;] = &#39;&#39;;
  }
  private function data2xml($xml, $data, $item = &#39;item&#39;){
    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;
  }
}
?>
로그인 후 복사

관련 권장 사항:

WeChat 공용 계정 결제에 대한 TP 액세스에 대한 자세한 설명

WeChat 공용 계정 개발 및 구성 시 일반적인 오류 메시지 요약

WeChat 공개 계정 구현 사용자 관리 기능

위 내용은 PHP WeChat 공개 계정 인증 토큰, 답변 내용, 푸시 메시지 방식의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!