|
<? Php
Class Wechat {
/**
* Pushed data or response data
* @ Var array
*/
Private $ data = array ();
/**
* Constructor, used to instantiate the SDK
* @ Param string $ token the TOKEN set for the token 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 the pushed data
* @ Return the data after array Conversion
*/
Public function request (){
Return $ this-> data;
}
/**
** Response Message (automatic reply)
* @ Param string $ to receive the user name
* @ Param string $ from the sender's username
* @ Param array $ content: the reply message. The text information is of the string type.
* @ Param string $ type Message type
* @ Param string $ flag indicates whether the new logo 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 the data to XML */
$ Xml = new SimpleXMLElement ('<xml> </xml> ');
$ This-> data2xml ($ xml, $ this-> data );
Exit ($ xml-> asXML ());
}
/**
* Reply to text information
* @ Param string $ content the information to be replied
*/
Private function text ($ content ){
$ This-> data ['content'] = $ Content;
}
/**
* Reply to music information
* @ Param string $ the music to be replied to by content
*/
Private function music ($ music ){
List (
$ Music ['title'],
$ Music ['description'],
$ Music ['musicurl'],
$ Music ['hqmusicurl']
) = $ Music;
$ This-> data ['music'] = $ Music;
}
/**
* Reply to text information
* @ Param string $ text content to be replied to by news
*/
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;} // a maximum of 10 news calls are allowed.
}
$ This-> data ['articlescount'] = count ($ articles );
$ This-> data ['articles '] = $ Articles;
}
/**
* Data XML Encoding
* @ Param object $ xml XML object
* @ Param mixed $ data
* @ Param string $ the node name of the item numeric index
* @ 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 sub-elements */
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 and authenticate the data to ensure that the data is sent.
* @ Param string $ token the TOKEN set for the token Open Platform
* @ Return boolean true-correct signature, false-Incorrect Signature
*/
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;
}
}
|