Share a PHP WeChat interface encapsulation class

Source: Internet
Author: User
Share a PHP interface encapsulation class Weixin_run (); // execution receiver Method class wechatCallbackapiTest {private $ fromUsername; private $ toUsername; private $ times; private $ keyword; public function weixin_run () {$ this-> responseMsg (); $ arr [] = array ("very reliable, good! "," Aa "," http://www.chuangen.com/pic/hj/s_4.png "," http://www.baidu.com "); $ Arr [] = array (" very reliable, good! "," Aa "," http://www.chuangen.com/pic/hj/s_4.png "," http://www.baidu.com "); $ Arr [] = array (" very reliable, good! "," Aa "," http://www.chuangen.com/pic/hj/s_4.png "," http://www.baidu.com "); $ This-> fun_xml (" news ", $ arr, array (2, 0);} function t ($ n) {include (" t_api.php "); $ c_name = $ t_api [$ n]; $ json = file_get_contents (" http://m.weather.com.cn/data/101110310.html "); Return json_decode ($ json);} public function valid () {$ echoStr = $ _ GET [" echostr "]; // valid signature, optionif ($ this-> checkSignature () {echo $ echoStr; exit;} public function responseMsg () {$ postStr = $ GLOBALS ["HTTP_RAW_POST_DATA"]; if (! Empty ($ postStr) {$ postObj = simplexml_load_string ($ postStr, 'simplexmlelement', LIBXML_NOCDATA); $ this-> fromUsername = $ postObj-> FromUserName; $ this-> toUsername = $ postObj-> ToUserName; $ this-> keyword = trim ($ postObj-> Content); $ this-> times = time ();} else {echo "this a file for weixin API! "; Exit ;}/// encapsulation class, // type: text type, news text type // text, array (content), array (ID) // news, array (title, introduction, image, hyperlink ),... less than 10), array (number, ID) private function fun_xml ($ type, $ value_arr, $ o_arr = array (0 )) {// ============================ xml header ===================$ con =" {$this->fromUsername} {$this->toUsername} {$ This-> times} {$type} "; // ============================ Type content ============== switch ($ type) {case "text": $ con. =" {$value_arr[0]} {$ O_arr} "; Break; case" news ": $ con. =" {$ o_arr [0]}"; Foreach ($ value_arr as $ id = >$ v) {if ($ id >=$ o_arr [0]) break; else null; // judge whether the number of arrays cannot exceed the set number $ con. =" <! [CDATA [{$ v [0]}]> {$v[1]} {$v[2]} {$v[3]} ";}$ Con. =" {$ O_arr [1]} "; Break ;} // end switch // ============================== end return ============ echo $ con." ";} Private function checkSignature () {$ signature = $ _ GET [" signature "]; $ timestamp = $ _ GET [" timestamp "]; $ nonce = $ _ GET ["nonce"]; $ token = TOKEN; $ tmpArr = array ($ token, $ timestamp, $ nonce); sort ($ tmpArr ); $ tmpStr = implode ($ tmpArr); $ tmpStr = sha1 ($ tmpStr); if ($ tmpStr ==$ signature) {return true ;} else {return false ;}}}?>

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.