Php implements the SDK (demo) extension method based on the WeChat public platform. sdkdemo_PHP tutorial

Source: Internet
Author: User
Php implements sdkdemo extension based on the public platform development SDK (demo. Php implements the SDK extension method based on the public platform development SDK (demo). sdkdemo this article describes how php implements the SDK extension method based on the public platform development SDK (demo. We will share with you the sdkdemo method for implementing SDK (demo) extension based on the public platform for php.

This article describes how to implement php SDK (demo) extension based on the public platform. Share it with you for your reference. The specific analysis is as follows:

This extension is based on the official public Platform SDK. it is simply encapsulated to implement some basic functions (such as weather query, translation, automatic chatbots, and custom menu interfaces) for learning only. the code is as follows:

The code is as follows:

Define ("TOKEN", "xingans ");
$ WechatObj = new wechatCallbackapiTest ();
$ WechatObj-> responseMsg ();
Class wechatCallbackapiTest
{
// Public API for signature verification
Public function valid ()
{
$ EchoStr = $ _ GET ["echostr"];
If ($ this-> checkSignature ()){
Echo $ echoStr;
Exit;
}
}

// Main entry handler
Public function responseMsg ()
{
$ PostStr = $ GLOBALS ["HTTP_RAW_POST_DATA"];
If (! Emptyempty ($ postStr )){
$ PostObj = simplexml_load_string ($ postStr, 'simplexmlelement', LIBXML_NOCDATA );
$ MsgType = trim ($ postObj-> MsgType );

Switch ($ msgType ){
Case 'text ':
$ ResultStr = $ this-> handleText ($ postObj );
Break;
Case 'event ':
$ ResultStr = $ this-> handleEvent ($ postObj );
Break;
Default:
$ ResultStr = $ this-> handleDefault ($ postObj );
Break;
}
Echo $ resultStr;

} Else {
Echo "Error ";
Exit;
}
}

// Process text messages
Private function handleText ($ obj ){
$ Keyword = trim ($ obj-> Content );
If (preg_match ('/weather/', $ keyword )){
$ ContentStr = $ this-> handleWeather ($ obj );
} Elseif (preg_match ('/Translation/', $ keyword )){
$ ContentStr = $ this-> handleTranslation ($ obj );
} Else {
$ ContentStr = $ this-> handleChat ($ obj );
}
Return $ this-> handleStr ($ obj, $ contentStr );
}

// Process the weather
Private function handleWeather ($ obj ){
$ Keyword = mb_substr ($ obj-> Content,-2, 2, 'utf-8 ');
$ Zone = mb_substr ($ obj-> Content, 0,-2, 'utf-8 ');
If ($ keyword = 'weather '&&! Emptyempty ($ zone )){
$ ZoneArr = json_decode (file_get_contents ('http: // api.k780.com: 88 /? App = weather. city & format = json'), true );
$ ZoneArr = $ zoneArr ['result'];
$ CityId = '';
Foreach ($ zoneArr as $ value ){
If ($ zone = $ value ['citynm ']) {
$ CityId = $ value ['weaid '];
Break;
}
}

If (! Emptyempty ($ cityId )){
$ Data = file_get_contents ("http://api.k780.com: 88 /? App = weather. today & weaid = $ cityId & appkey = 10638 & sign = 3736578f099375665f9f141a6326b757 & format = json ");
$ Data = json_decode ($ data );
$ ContentStr = "today is :". $ data-> result-> days. ",". $ data-> result-> week. ",". $ data-> result-> citynm. "Weather :". $ data-> result-> weather. "n temperature :". $ data-> result-> temperature. ",
". $ Data-> result-> wind_direction. ",". $ data-> result-> wind_power. ", minimum temperature :". $ data-> result-> temp_low. ", maximum temperature :". $ data-> result-> temp_high;
} Else {
$ ContentStr = 'The input City cannot be found! ';
}
} Else {
$ ContentStr = 'incorrect Query format! ';
}
Return $ contentStr;
}

// Process translation
Private function handleTranslation ($ obj ){
$ Keyword = mb_substr ($ obj-> Content, 0, 2, 'utf-8 ');
$ Words = mb_substr ($ obj-> Content, 2,220, 'utf-8 ');
If ($ keyword = 'translation '&&! Emptyempty ($ words )){
$ Data = file_get_contents ('http: // fanyi.youdao.com/openapi.do? Keyfrom = zfsblog & key = 364295447 & type = data & doctype = json & version = 1.1 & q = '. urlencode ($ words ));
$ Data = json_decode ($ data, true );

Switch ($ data ['errorcode']) {
Case '0 ':
$ ContentStr = $ data ['translation'] [0];
Break;
Case '20 ':
$ ContentStr = 'long text to be translated ';
Break;
Case '30 ':
$ ContentStr = 'invalid translation ';
Break;
Case '40 ':
$ ContentStr = 'unsupported language type ';
Break;
Case '50 ':
$ ContentStr = 'invalid key ';
Break;
Default:
$ ContentStr = 'error ';
Break;
}
} Else {
$ ContentStr = 'The input translation format is incorrect! ';
}
Return $ contentStr;
}

// Process chat information
Private function handleChat ($ obj ){
$ Keywords = $ obj-> Content;
$ CurlPost = array ("chat" => $ keywords );
$ Ch = curl_init (); // initialize curl
Curl_setopt ($ ch, CURLOPT_URL, 'http: // www.xiaojo.com/bot/chata.php'); // capture the specified webpage
Curl_setopt ($ ch, CURLOPT_HEADER, 0); // Set the header
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1); // The result is a string and output to the screen.
Curl_setopt ($ ch, CURLOPT_POST, 1); // post submission method
Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ curlPost );
$ Data = curl_exec ($ ch); // run curl
Curl_close ($ ch );

If (! Emptyempty ($ data )){
$ ContentStr = $ data;
} Else {
$ Ran = rand (1, 5 );

Switch ($ ran ){
Case 1:
$ ContentStr = "John is tired today. I will chat with you tomorrow ";
Break;
Case 2:
$ ContentStr = "jiujiu sleeping Hello ~~ ";
Break;
Case 3:
$ ContentStr = "call ~~ Call ~~ ";
Break;
Case 4:
$ ContentStr = "You have a lot to talk about. don't talk to you ";
Break;
Case 5:
$ ContentStr = "You have a lot to talk about. don't talk to you ";
Break;
}
}
Return $ contentStr;
}

// Create a custom menu
Public function createMenu ($ data, $ token ){
$ Ch = curl_init ();
Curl_setopt ($ ch, CURLOPT_URL, "https://api.weixin.qq.com/cgi-bin/menu/create? Access_token = ". $ token );
Curl_setopt ($ ch, CURLOPT_CUSTOMREQUEST, "POST ");
Curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, false );
Curl_setopt ($ ch, CURLOPT_SSL_VERIFYHOST, false );
Curl_setopt ($ ch, CURLOPT_USERAGENT, 'mozilla/5.0 (compatible; MSIE 5.01; Windows NT 5.0 )');
Curl_setopt ($ ch, CURLOPT_FOLLOWLOCATION, 1 );
Curl_setopt ($ ch, CURLOPT_AUTOREFERER, 1 );
Curl_setopt ($ ch, CURLOPT_POSTFIELDS, $ data );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true );
$ Result = curl_exec ($ ch );
Curl_close ($ ch );
Return $ result;
}

// Query the custom menu
Function getMenu ($ token ){
$ Url = "https://api.weixin.qq.com/cgi-bin/menu/get? Access_token = $ token ";
$ Ch = curl_init ($ url );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true); // get data and return
Curl_setopt ($ ch, CURLOPT_BINARYTRANSFER, true); // when CURLOPT_RETURNTRANSFER is enabled, data is returned.
Curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, false );
Curl_setopt ($ ch, CURLOPT_SSL_VERIFYHOST, false );
Return $ output = curl_exec ($ ch );
}

// Delete a custom menu
Public function deleteMenu ($ token ){
$ Url = "https://api.weixin.qq.com/cgi-bin/menu/delete? Access_token = ". $ token;
$ Ch = curl_init ();
Curl_setopt ($ ch, CURLOPT_URL, $ url );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
Curl_setopt ($ ch, CURLOPT_SSL_VERIFYPEER, false );
Curl_setopt ($ ch, CURLOPT_SSL_VERIFYHOST, false );
$ Result = curl_exec ($ ch );
Curl_close ($ ch );
Return $ result;
}

// Process event messages
Private function handleEvent ($ obj ){
$ Content = '';
Switch ($ obj-> Event ){
Case 'subscribe ':
$ Content. = "welcome-welcome to follow this public account! ";
Break;
Case 'unsubscribe ':
$ Content. = "thank you for your attention to this public account. goodbye! ";
Break;
Default:
$ Content. = "";
Break;
}
Return $ this-> handleStr ($ obj, $ content );
}

// Process the reply message string
Private function handleStr ($ obj, $ content = '', $ flag = 0 ){
$ TextTpl = "";
Return sprintf ($ textTpl, $ obj-> FromUserName, $ obj-> ToUserName, time (), $ content, $ flag );
}

// Signature verification function
Private function checkSignature ()
{
$ Signature = $ _ GET ["signature"];
$ Timestamp = $ _ GET ["timestamp"];
$ Nonce = $ _ GET ["nonce"];
$ Token = TOKEN;
$ TmpArr = array ($ token, $ timestamp, $ nonce );
Sort ($ tmpArr, SORT_STRING );
$ TmpStr = implode ($ tmpArr );
$ TmpStr = sha1 ($ tmpStr );
If ($ tmpStr = $ signature ){
Return true;
} Else {
Return false;
}
}
}

I hope this article will help you with php programming.

SDK demo describes how php implements SDK extension based on the public platform. Share it with you...

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.