This article describes the PHP Micro-trust public platform development class. Share to everyone for your reference. The specific analysis is as follows:
The thinkwechat.php class files are as follows:
<?php class WeChat {/** * micro-letter pushed over data or response data * @var array/private $data = Array (); /** * Constructs a method for instantiating the micro-letter SDK * @param string $token The token/Public function __construct ($token) {$this of the micro-letter open platform setting >auth ($token) | |
Exit
if (!empty ($_get[' echostr ')) {exit ($_get[' echostr ']);
else {try {$xml = file_get_contents ("Php://input");
$xml = new SimpleXMLElement ($xml); $xml | |
Exit
foreach ($xml as $key => $value) {$this->data[$key] = Strval ($value); }catch (Exception $e) {}}}/** * Get micro-mail push data * @return Array transform the data after the array/public function
Request () {return $this->data; /** * * Response to Micro-letter message (Automatic Reply) * @param string $to receive username * @param string $from sender username * @param array $content
Reply information, text information is string type * @param string $type Message type * @param string $flag whether the new label just received * @return string XML string */Public Function response ($content, $type = ' text ', $flag = 0) {/* Basic data * * * $this->data = array (' tousername ' => $this->data[' F Romusername '], ' fromusername ' => $this->data[' tousername '], ' createtime ' => time (), ' Msgtype '
=> $type,);
/* Add Type Data * * * $this-> $type ($content);
/* Add status/$this->data[' funcflag '] = $flag;
/* Convert data to XML/$xml = new SimpleXMLElement (' <xml></xml> ');
$this->data2xml ($xml, $this->data);
Exit ($xml->asxml ()); /** * Reply Text message * @param string $content the message to reply * * Private function text ($content) {$this->data[' Conten
T '] = $content; /** * Reply to Music message * @param string $content Music to reply/private function music ($music) {list ($music [' Tit
Le '], $music [' Description '], $music [' Musicurl '], $music [' hqmusicurl ']) = $music;
$this->data[' Music '] = $music; /** * Reply to graphic information * @param string $news text content to reply/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;}///Up to only 10 tune news} $this->data[' articlecount ' = count ($articles);
$this->data[' articles '] = $articles; /** * Data XML Encoding * @param object $xml XML Object * @param mixed $data data * @param string $item A numeric index node name * @re
Turn string/Private function Data2xml ($xml, $data, $item = ' item ') {foreach ($data as $key => $value) {
/* Specifies the default number key/Is_numeric ($key) && $key = $item;
/* Add child element */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)); /** * Authenticates the data to ensure that it is a micro-letter sent data * @param string $token token * @return Boolean true-for the micro-letter open platform setting
Correct signature, false-signature error/Private function auth ($token) {if (Empty ($_get[' signature ')) return;
* * Get Data/$data = Array ($_get[' timestamp '], $_get[' nonce '), $token);
$sign = $_get[' signature '];
/* Sort the data in dictionary/Sort ($data, sort_string);
/* Generate signature */$signature = SHA1 (implode ($data));
return $signature = = = $sign; }
}
I hope this article will help you with your PHP programming.