Php WeChat public platform development instance, php public platform instance _ PHP Tutorial

Source: Internet
Author: User
Php public platform development instance and php public platform instance. Php public platform development example, php public platform example this article describes the php public platform development class. Share it with you for your reference. The specific analysis is as follows: ThinkWecha php public platform development instance, 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 ('
 '); $ 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.

Examples in this article describe php public platform development. Share it with you for your reference. The specific analysis is as follows: ThinkWecha...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.