The following changes will occur to the public platform due to interface upgrade. When a new user subscribes, the previous "Hello2BizUser" text will be pushed and changed to a "subscribe" event. Recently, I met several friends who asked new users how to change the code after subscribing to an event. now I will explain the process in detail as follows:
The following changes will occur to the public platform due to interface upgrade. When a new user subscribes, the previous "Hello2BizUser" text will be pushed and changed to a "subscribe" event. Recently, I met several friends who asked the new user how to change the code after subscribing to the event. now I will explain the detailed process as follows:
I. handling Hello2BizUser old events
In the old follow events, after a user pays attention to the public platform account, the system will send a Hello2BizUser text to the public account, in the background development mode of a public account, you can use Hello2BizUser to send a welcome word.
The sample code is as follows:
If ($ keyword = "Hello2BizUser "){
$ ContentStr = "Welcome to fanwe studio. this is an old welcome word. you cannot receive it ";
$ ResultStr = $ this-> transmitText ($ object, $ contentStr, $ funcFlag );
Return $ resultStr;
}
Modifying the basic interface will affect everyone. It is generally not easy to make such changes.
The disadvantage of this method is that if the user does not judge the event, there will be no welcome words. it does not matter, it does not affect the appearance of a welcome word. However, in many people's program code, all processes are directly judgment keywords. For example, we once saw a hospital account. when a user sends a registration number, the number of people listed above is displayed. However, the background program did not differentiate the number and sent Hello2BizUser as a registration ticket, the registration "Hello2BizUser" is not found, and the number of people in front is unknown. In addition, if a user actively sends a Hello2BizUser in the past, it will also get the same content as the welcome word, although few users will send this content.
On the other hand, making user attention an event is more conducive to the implementation of the statistical function. With this event, we can easily determine the number of followers and the number of Unsubscribe users. However, the original Hello2BizUser text push judgment may be inaccurate because users can manually send messages, generate false attention statistics.
II. "subscribe" subscription event judgment
Subscribe is a new event. First, we need to determine the event type. in the official example, we add a judgment on this event. the modifications are as follows:
$ PostObj = simplexml_load_string ($ postStr, 'simplexmlelement', LIBXML_NOCDATA );
$ RX_TYPE = trim ($ postObj-> MsgType );
Switch ($ RX_TYPE)
{
Case "text ":
$ ResultStr = $ this-> deleetext ($ postObj );
Break;
Case "event ":
$ ResultStr = $ this-> receiveEvent ($ postObj );
Break;
Default:
$ ResultStr = "unknow msg type:". $ RX_TYPE;
Break;
}
Then, the subscribe event is judged in the event receiving and processing function:
Private function receiveEvent ($ object)
{
$ ContentStr = "";
Switch ($ object-> Event)
{
Case "subscribe ":
$ ContentStr = "Hello, welcome to Fang times studio. New feeling, new experience! ";
Break;
}
$ ResultStr = $ this-> transmitText ($ object, $ contentStr );
Return $ resultStr;
}
In this way, the subscribe event is processed.
II. complete code
Define ("TOKEN", "Fang times studio ");
$ WechatObj = new wechatCallbackapiTest ();
$ WechatObj-> responseMsg ();
Class wechatCallbackapiTest
{
Public function responseMsg ()
{
$ PostStr = $ GLOBALS ["HTTP_RAW_POST_DATA"];
If (! Empty ($ postStr )){
$ PostObj = simplexml_load_string ($ postStr, 'simplexmlelement', LIBXML_NOCDATA );
$ RX_TYPE = trim ($ postObj-> MsgType );
Switch ($ RX_TYPE)
{
Case "text ":
$ ResultStr = $ this-> deleetext ($ postObj );
Break;
Case "event ":
$ ResultStr = $ this-> receiveEvent ($ postObj );
Break;
Default:
$ ResultStr = "unknow msg type:". $ RX_TYPE;
Break;
}
Echo $ resultStr;
} Else {
Echo "";
Exit;
}
}
Private function deleetext ($ object)
{
$ FuncFlag = 0;
$ Keyword = trim ($ object-> Content );
$ ResultStr = "";
$ CityArray = array ();
$ ContentStr = "";
$ NeedArray = false;
$ Illegal = false;
$ Saytome = false;
If ($ keyword = "Hello2BizUser "){
$ ContentStr = "Welcome to fanwe studio. this is an old welcome word. you cannot receive it when paying attention to it ";
$ ResultStr = $ this-> transmitText ($ object, $ contentStr, $ funcFlag );
Return $ resultStr;
} Else {
}
}
Private function receiveEvent ($ object)
{
$ ContentStr = "";
Switch ($ object-> Event)
{
Case "subscribe ":
$ ContentStr = "Hello, welcome to Fang times studio. New feeling, new experience! ";
Break;
}
$ ResultStr = $ this-> transmitText ($ object, $ contentStr );
Return $ resultStr;
}
Private function transmitText ($ object, $ content, $ flag = 0)
{
$ TextTpl ="
%s
%s
% S
text
%s
% D
";
$ ResultStr = sprintf ($ textTpl, $ object-> FromUserName, $ object-> ToUserName, time (), $ content, $ flag );
Return $ resultStr;
}
}
?>