Directly use: PHP to encapsulate WeChat interfaces

Source: Internet
Author: User
Public platform interface PHP100 encapsulation function files are directly used: PHP encapsulation interface /**
* Public platform interface PHP100 encapsulates function files
* Author
* Date 2013-4-9 PRC: E + 8
*/
 
Define ("TOKEN", "chuangenit ");
$ WechatObj = new wechatCallbackapiTest ();
$ WechatObj-> weixin_run (); // executes the 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, option
If ($ 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),... smaller than 10), array (number of items, 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; // determines that 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.