Verify the php code of token, reply to text, and push messages,

Source: Internet
Author: User

Verify the token php code of the token, reply to text, and push messages,

The examples in this article share with you the classes used to verify tokens, reply text and text, and push messages to users. The specific code is as follows:

<?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;  }}?>

The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

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.