Php public platform development instance and php public platform instance
This example describes the php public platform development. Share it with you for your reference. The specific analysis is as follows:
The ThinkWechat. php file is as follows:
<? Phpclass Wechat {/*** pushed data or response data * @ var array */private $ data = array ();/*** constructor, used to instantiate the SDK * @ param string $ token TOKEN */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 the pushed data * @ return array the data converted to an array */public function request () {return $ this-> data ;} /***** Response Message (automatic reply) * @ param string $ to receiving username * @ param string $ from sender username * @ param array $ content reply message, the text information is string type * @ param string $ type the Message type * @ param string $ flag indicates whether the received information is newly marked * @ 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 ['fundeflag'] = $ flag; /* convert the data to XML */$ xml = new SimpleXMLElement ('<xml> </xml>'); $ this-> data2xml ($ xml, $ this-> data); exit ($ xml-> asXML ());} /*** reply text information ** @ param string $ content the information to be replied to */private function text ($ content) {$ this-> data ['content'] = $ Content ;} /*** reply music information ** @ param string $ content the music to be replied to */private function music ($ music) {list ($ music ['title'], $ music ['description'], $ music ['musicurl'], $ music ['hqmusicurl']) = $ music; $ this-> data ['music'] = $ Music ;} /*** reply to text information ** @ param string $ news the text to be replied */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 10 news calls allowed} $ this-> data ['articlecount'] = count ($ articles ); $ this-> data ['articles '] = $ Articles ;} /*** data XML encoding ** @ param object $ xml XML object * @ param mixed $ data * @ param string $ item: the node name in the digital 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 sub-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) ;}}/ *** sign the data, make sure the sent data * @ param string $ token the TOKEN set by the open platform * @ return boolean true-the signature is correct, false-the signature is incorrect */private function auth ($ token) {if (empty ($ _ GET ['signature']) return;/* GET data */$ data = array ($ _ GET ['timestamp'], $ _ GET ['nonce '], $ token); $ sign = $ _ GET ['signature'];/* sort data alphabetically */sort ($ data, SORT_STRING);/* generate a signature */$ signature = sha1 (implode ($ data); return $ signature ===$ sign ;}}
I hope this article will help you with php programming.