Home > Article > Backend Development > PHP WeChat public platform development class example_PHP tutorial
The ThinkWechat.php class file is as follows:
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 43 44 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 84 85 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 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
class Wechat { /** * Data or response data pushed by WeChat * @var array */ private $data = array(); /** * Construction method, used to instantiate WeChat SDK * @param string $token TOKEN set by WeChat open platform */ 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 data pushed by WeChat * @return array Data converted to array */ public function request(){ return $this->data; } /** * * Respond to messages sent by WeChat (automatic reply) * @param string $to receives 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 standard has just received the 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 The message to reply */ private function text($content){ $this->data['Content'] = $content; } /** * Reply to music message * @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 messages * @param string $news The graphic and text content to be replied 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; } //Only 10 news updates are allowed at most } $this->data['ArticleCount'] = count($articles); $this->data['Articles'] = $articles; } /** * Data XML encoding * @param object $xml XML object * @param mixed $data data * @param string $item Node name when numerically indexed * @return string */ private function data2xml($xml, $data, $item = 'item') { foreach ($data as $key => $value) { /* Specify the default numeric 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)); } } } } /** * Perform signature authentication on the data to ensure it is the data sent by WeChat * @param string $token TOKEN set by WeChat open platform * @return boolean true-signature is correct, false-signature is wrong */ private function auth($token){ if(empty($_GET['signature'])) return; /* Get data */ $data = array($_GET['timestamp'], $_GET['nonce'], $token); $sign = $_GET['signature']; /* Dictionary sort the data */ sort($data,SORT_STRING); /* Generate signature */ $signature = sha1(implode($data)); return $signature === $sign; } } |