1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26-27--28 29---30 31--32 33 34 35 36 37 38-39 40 41 42 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 5, 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 11 9 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141-142 |
<?php class WeChat {/** * micro-letter pushed over data or response data * @var array/private $data = Array ();/** * Construct method for instantiating the micro-letter SDK * @param stri ng $token Micro-Credit open platform token/public function __construct ($token) {$this->auth ($token) | | | exit; if (!empty $_get[' Echost R ']) {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 the data from the micro-mail push * @return Array transforms 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 class Type * @param string $type Message type * @param string $flag whether the new label just received * @return string XML String */Public Function response ($conten T, $type = ' text ', $flag = 0) {/* basic data */$this->data = Array (' tousername ' => $this->data[' fromusername '), ' from UserName ' => $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 message to reply * * Private function text ($content) {$this->data[' content '] = $content;} /** * Reply to Music message * @param string $content Music to reply/private function music ($music) {list ($music [' Title '], $music [' Descriptio N '], $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 * @return String/PR ivate 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); $nod E = Dom_import_simplexml ($child); $node->appendchild ($node->ownerdocument->createcdatasection ($value)); /** * Authentication of data to ensure that it is a micro-letter sent data * @param string $token Micro-letter Open Platform Set token * @return Boolean true-signature is correct, false-signature Error * * * Private function Auth ($token) {if (Empty ($_get[' signature ')) return;/* FETCH 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; } } |