Class Wechat { /** * Push-over data or response data * @var Array */ Private $data = Array (); /** * Constructor method for instantiating the SDK * @param string $token token for open platform settings */ Public function __construct ($token) { $this->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 Push data * @return Array to convert data to arrays */ Public Function request () { return $this->data; } /** * * Respond to messages sent (auto reply) * @param string $to receive user name * @param string $from Sender user Name * @param array $content reply message, text message is string type * @param string $type message type * @param string $flag Whether the newly received information * @return String XML string */ Public Function response ($content, $type = ' text ', $flag = 0) { /* Basic Data */ $this->data = Array ( ' Tousername ' = $this->data[' fromusername '], ' 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 (" ); $this->data2xml ($xml, $this->data); Exit ($xml->asxml ()); } /** * Reply to text message * @param string $content information to reply to */ Private function Text ($content) { $this->data[' Content ' = $content; } /** * Reply to music information * @param string $content The music to reply to */ Private function Music ($music) { List $music [' Title '], $music [' Description '], $music [' Musicurl '], $music [' Hqmusicurl '] ) = $music; $this->data[' Music ') = $music; } /** * Reply to graphic information * @param string $news text content to reply to */ 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 news is allowed } $this->data[' articlecount '] = count ($articles); $this->data[' articles ') = $articles; } /** * Data XML encoding * @param an object $xml XML object * @param mixed $data data * Node name when @param string $item a numeric index * @return String */ Private Function Data2xml ($xml, $data, $item = ' item ') { foreach ($data as $key = = $value) { /* Specify the default number key */ Is_numeric ($key) && $key = $item; /* Add child elements */ 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)); } } } } /** * Signature verification of data to ensure that data is sent * @param string $token token for open platform settings * @return Boolean true-signed correctly, 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 a dictionary */ Sort ($data, sort_string); /* Generate signature */ $signature = SHA1 (implode ($data)); return $signature = = = $sign; } } |