This article is an entry-level public platform development tutorial. For more information, see the topic, this article is an entry-level public platform development tutorial. For more information, see
In this Getting Started Tutorial, we assume that you already have PHP language programs, MySQL databases, computer network communication, and XML language basics. If you do not have one, learn the relevant knowledge first.
We will use the public account Fang times studio (account: pondbaystudio, QR code at the bottom) as an example to explain.
This tutorial will guide you through the following tasks:
Create baidu cloud platform application enable public platform development mode to get subscription, text, image, voice, video message reply text, text and music message program development
Create a Baidu Cloud Application
Apply for an account
Log on to developer.baidu.com/bae and register an account using your email or mobile phone. you must bind your mobile phone and verify your email at the same time.
Create an application
ClickUpload code
Change the token in the following code to your name and save it as index. php.
Note: the Token entered here will be used in the following URL. You can save it first.
define("TOKEN", "pondbay");$wechatObj = new wechatCallbackapiTest();$wechatObj->valid();class wechatCallbackapiTest{ public function valid() { $echoStr = $_GET["echostr"]; if($this->checkSignature()){ echo $echoStr; exit; } } 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; } }}?>
And then compress it into zip format
Send text
Background format:
gh_680bdefc8c5d
oIDrpjqASyTPnxRmpS9O_ruZGsfk
1359028446
text
你好
5836982729904121631
Send emoticons
Background format
gh_680bdefc8c5d
oIDrpjqASyTPnxRmpS9O_ruZGsfk
1359044526
text
/::)/::)/::)/::)/::)
5837051792978241864
XML Format Description
ToUserName: message recipient number, generally the public platform account number.
FromUserName message sender ID
CreateTime message creation time
MsgType message type; text message is text
Content message Content
MsgId indicates the message id. the Message types of text and emoticon are both text.
[Html]
2. images
Send Image
Background format:
[Code]
gh_680bdefc8c5d
oIDrpjqASyTPnxRmpS9O_ruZGsfk
1359028479
image
http://www.php.cn/
5836982871638042400
XML Format Description
ToUserName: message recipient number, generally the public platform account number.
FromUserName message sender ID
CreateTime message creation time
MsgType message type; image message
PicUrl url, which can be obtained through HTTP GET
MsgId message ID
3. voice
Send Voice
Background format:
gh_680bdefc8c5d
oIDrpjqASyTPnxRmpS9O_ruZGsfk
1359028025
voice
hGm9wmKth8RO_tuv5k9fJkSbovXWzZVYwG2jSsL7ukCqq6q1SiLzYnFEngFNUijs
amr
5836980921722890003
XML Format Description
ToUserName: message recipient number, generally the public platform account number.
FromUserName message sender ID
CreateTime message creation time
MsgType message type; voice message is voice
MediaId media ID
Format: Voice Format. here, amr is used.
MsgId: Description of AMR interface
The full name is Adaptive Multi-Rate, which is mainly used for audio from mobile devices. the compression ratio is relatively large, but the compression quality is poor compared to other compression formats. because it is mostly used for voice and call, the effect is still very good.
4. Video
Send Video
Background format:
gh_680bdefc8c5d
oIDrpjqASyTPnxRmpS9O_ruZGsfk
1359028186
video
DBVFRIj29LB2hxuYpc0R6VLyxwgyCHZPbRj_IIs6YaGhutyXUKtFSDcSCPeoqUYr
mxUJ5gcCeesJwx2T9qsk62YzIclCP_HnRdfTQcojlPeT2G9Q3d22UkSLyBFLZ01J
5836981613212624665
XML Format Description
ToUserName: message recipient number, generally the public platform account number.
FromUserName message sender ID
CreateTime message creation time
MsgType message type; video message
MediaId media ID
ThumbMediaId media scaling ID?
MsgId message ID original: http://www.php.cn/
5. location
Sending location
Background format:
gh_680bdefc8c5d
oIDrpjqASyTPnxRmpS9O_ruZGsfk
1359036619
location
22.539968
113.954980
16
中国广东省深圳市南山区深南大道9789号 邮政编码: 518057
5837017832671832047
XML Format Description
ToUserName: message recipient number, generally the public platform account number.
FromUserName message sender ID
CreateTime message creation time
MsgType message type, location
Location_X geographic location and latitude
Location_Y geographic location longitude
Scale the map size
Label location information
MsgId message ID
6. links
Send link
Background format:
gh_680bdefc8c5d
oIDrpjl2LYdfTAM-oxDgB4XZcnc8
1359709372
link
<! [CDATA [share an application with you-Fang times Magic Map]>
方倍神图为您提供夫妻相测试,亲子鉴定等新奇、好玩的图片测试功能
http://www.php.cn/
5839907284805129867
XML Format Description
ToUserName: message recipient number, generally the public platform account number.
FromUserName message sender ID
CreateTime message creation time
MsgType message type, link: link
Title text message Title
Description text message Description
Url click the text message jump link
MsgId message ID
Message sending type
Currently, public accounts can push messages in three formats to common users: text, text, and music. Text messages include single text messages and multiple text messages. the display method is slightly different.
In addition, the reply message supports the Star action: After you set the FuncFlag field to 1 to perform the Star action on the reply message, you can find the corresponding push message in the Star message category of the real-time message.
The following details are as follows:
1. Text Message format
Reply text
Background format:
oIDrpjqASyTPnxRmpS9O_ruZGsfk
gh_680bdefc8c5d
1359036631
text
★★★★豆立方★★★★ 公众平台上最好玩的图片及视频应用
0
XML Format Description
FromUserName message sender
ToUserName message receiver
CreateTime message creation time
MsgType message type. text messages must be in text
Content message Content. The size is limited to 2048 bytes. The field is empty and the request is invalid.
FuncFlag star field
2. Text Message format
2.1 single text message
Reply to a single image
Background format:
oIDrpjqASyTPnxRmpS9O_ruZGsfk
gh_680bdefc8c5d
1359011899
news
1
<! [CDATA [[Tianjin] Weather conditions]>
温度:3℃ 湿度:43﹪ 风速:西南风2级
http://www.php.cn/
0
Over 2.2 text messages
oIDrpjqASyTPnxRmpS9O_ruZGsfk
gh_680bdefc8c5d
1359011829
news
8
<! [CDATA [[Tianjin] weather condition temperature: 3 ℃ humidity: 43 wind speed: 2 levels of southwest wind]>
http://www.php.cn/
<! [CDATA [-on Thursday, June 24 ~ -7 deg C, clear north wind 3-4 to southeast wind less than 3]>
http://www.php.cn/
<! [CDATA [Friday, January 1, June 25-1 ℃ ~ -The Southeast wind of 8 ℃ is less than 3 levels to the northeast wind 3-4 levels]>
http://www.php.cn/
<! [CDATA [Saturday, January 1, June 26-1 ℃ ~ -7 ℃ multi-cloud northeast wind 3-4 to southeast wind less than 3]>
http://www.php.cn/
<! [CDATA [Sunday, January 1, June 27, 0 ℃ ~ -6 ℃ multi-cloud southeast wind is less than 3 levels to northeast wind 3-4 levels]>
http://www.php.cn/
<! [CDATA [Monday, January 1, June 28-1 ℃ ~ -8 ℃ multi-cloud northeast wind 3-4 to south wind less than 3]>
http://www.php.cn/
<! [CDATA [Tuesday 1 c ~ -5 °c multi-cloud south wind is less than 3 levels to 3-4 levels]>
http://www.php.cn/
<! [CDATA [All rights reserved by Fang times studio]>
0
XML Format Description
FromUserName message sender
ToUserName message receiver
CreateTime message creation time
MsgType message type. the Message Type must be news.
Content message Content. text messages can be left blank
ArticleCount: number of text and text messages, up to 10
Multiple text message entries in Articles. The first item is a large image by default.
Title text message Title
Description text message Description
PicUrl Image links. JPG and PNG formats are supported. the better results are 40*320 and 80x80.
Url click the text message jump link
FuncFlag star field
3. music messages
Background format:
ollB4jqgdO_cRnVXk_wRnSywgtQ8
gh_b629c48b653e
1372310544
music
<! [CDATA [the most dazzling national style]>
凤凰传奇
http://www.php.cn/
http://www.php.cn/
0
XML Format Description
ToUserName receiver account (received OpenID)
FromUserName developer id
CreateTime message creation time
MsgType message type, which is music
Title Music Title
Description Music Description
MusicUrl Music link
HQMusicUrl: high-quality music link, which is preferentially used to play music in WIFI
When FuncFlag 0x0001 is marked, the star just received the message.
Event message type
Currently, when users follow and cancel the following notifications, they will automatically send event push messages to the public platform:
1. follow events
gh_b629c48b653e
ollB4jv7LA3tydjviJp5V9qTU_kA
1372307736
event
subscribe
2. cancel follow events
gh_b629c48b653e
ollB4jqgdO_cRnVXk_wRnSywgtQ8
1372309890
event
unsubscribe
3. menu click event
gh_680bdefc8c5d
oIDrpjqASyTPnxRmpS9O_ruZGsfk
1377886191
event
CLICK
天气深圳
XML Format Description
ToUserName recipient number
FromUserName sender ID. if it is a common user, it is an OpenID
CreateTime message creation time
MsgType message type, event
Event type, subscribe, unsubscribe, and CLICK)
EventKey event KEY value, which corresponds to the KEY value in the custom menu interface
Simple automatic reply
We made some modifications in the official example to implement a message "?" You can reply to the current time function.
In this example, a text message ($ postObj-> Content) is received and a text message ($ msgType = "text";) is returned.
You can save the following code as index. php and re-upload it as shown above.
The code is as follows:
define("TOKEN", "pondbay");$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); $fromUsername = $postObj->FromUserName; $toUsername = $postObj->ToUserName; $keyword = trim($postObj->Content); $time = time(); $textTpl = "
%s
%s
%s
%s
%s
0
"; if($keyword == "?") { $msgType = "text"; $contentStr = date("Y-m-d H:i:s",time()); $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr; } }else{ echo ""; exit; } }}?>
The effect is as follows:
For more public platform development tutorials (graphic details), please follow the PHP Chinese network!