Home >WeChat Applet >WeChat Development >Detailed example of WeChat development account based on IBM Bluemix
Abstract: IBM has released Bluemix, and the number of WeChat public platform accounts has reached more than 2 million. If Bluemix can be used to develop WeChat public platform applications, this will be great news for the majority of WeChat developers. .
[Editor's Note] IBM recently released Bluemix, a platform-as-a-service (PaaS) environment based on open standards for building, running, and managing web and mobile applications in the cloud. The number of WeChat public platform accounts has reached more than 2 million. Therefore, if Bluemix can be used to develop WeChat public platform applications, the author believes that it will bring good news to the majority of WeChat developers to learn, deploy, test, and form commercial applications. Therefore, the author passed After research, we successfully completed the application development of WeChat public platform based on Bluemix. Here we will make some brief introduction to facilitate WeChat public developers to promote in-depth application.
Before developing the WeChat public platform, you need to apply for a WeChat public account. I will not go into details here. The name of the WeChat public account used by the author is: "Zhenghai Shuo Knowledge Innovation" and the code name is: "zhszscx". I will not go into details on how to apply for a WeChat public account.
Developing the WeChat public platform in Bluemix mainly includes the following steps:
Apply for a Bluemix account
Download and install the Cloud Foundry program
Compile the configuration of an application File
Set the development mode on the WeChat public platform
Set a Bluemix-based URL
Set the token of the WeChat public platform application
Download the sample program of the WeChat public platform. The author uses PHP language to modify the program and set the token (TOKEN)
Upload (push) the PHP file and the application starts executing
In WeChat In the public platform, verification is through edit mode.
Use WeChat on your mobile phone to send information to the WeChat public platform and obtain the information returned by the WeChat public platform.
At this point, the development of the WeChat public platform application based on Bluemix has been successfully completed, and developers can carry out further development on this basis.
Figure 1. System framework
Process description
Apply for Bluemix account
Enter the URL: www.bluemix.net
Figure 2
##Download and install the Cloud Foundry program
Figure 3 Download the corresponding program according to the developer's program operating system. Figure 4 For example, the author's download address is: "Windows 64 bit" under Stable InstallersDownloaded a file, the The file is installer-windows-amd64 .zipAfter installing this file, a cf.exe file is generated under C:\Program Files (x86)\Cloud Foundry. At the same time, after inspection, it was found that the directory has also been added to the PATH path of the system, which means that the CF command can be directly entered in the DOS COMMAND environment to execute the file. Prepare an application configuration fileThe file name is: manifest.yml As an application, you need to set the name of the application and the URL generated by the application. The example file is:---applications:- name: cf-php-mysql1 memory: 256M instances: 1 host: lzhfirstphp domain: ng.bluemix.net path: . buildpack: <a href="https://github.com/dmikusa-pivotal/cf-php-build-pack.git">https://github.com/dmikusa-pivotal/cf-php-build-pack.git</a>Description: name specifies the name of the application host specifies the extended domain name of the application. For example, the above example file generates the domain name: lzhfirstphp.ng.bluemix .netNo other changes should be made. After the modification is completed, save the file. There are three things to note when preparing this file: The file format needs to be specified as UTF-8 formatWhen setting the application name, be careful not to repeat it. When setting the HOST name, make it as long as possible, or set it according to the personal name or company name. The author once made an error when uploading the deployment. It is estimated that the name is duplicated. After later modification, the deployment was successful. Set the development mode on the WeChat public platformEnter the WeChat public platform, log in, enter the advanced functions, and set the development mode. Figure 5 Figure 6
##In the WeChat public platform interface, set the corresponding URL and token.
Figure 7
Set a Bluemix-based URL
Set the URL in the URL. Note that the form here must be the same as the previous setting. consistent.
Set the token for the WeChat public platform application
Set a token information, which is specified by yourself, usually using special characters to ensure security .
下载微信公众平台的范例程序,笔者采用 PHP 语言,对该程序进行修改,设置令牌(token)等相关信息。开发者可以在微信公众平台开发者文档中下载 DEMO 文件。
图 8
清单 1
<?php/** * wechat php test *///define your tokendefine("TOKEN", "weixin");$wechatObj = new wechatCallbackapiTest();$wechatObj->valid();class wechatCallbackapiTest{public function valid() { $echoStr = $_GET["echostr"]; //valid signature , option if($this->checkSignature()){ echo $echoStr; exit; } } public function responseMsg() {//get post data, May be due to the different environments$postStr = $GLOBALS["HTTP_RAW_POST_DATA"]; //extract post dataif (!empty($postStr)){ $postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA); $fromUsername = $postObj->FromUserName; $toUsername = $postObj->ToUserName; $keyword = trim($postObj->Content); $time = time(); $textTpl = "<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[%s]]></MsgType><Content><![CDATA[%s]]></Content><FuncFlag>0</FuncFlag></xml>"; if(!empty( $keyword )) { $msgType = "text"; $contentStr = "Welcome to wechat world!"; $resultStr = sprintf($textTpl, $fromUsername, $toUsername, $time, $msgType, $contentStr); echo $resultStr; }else{ echo "Input something..."; } }else { echo ""; exit; } }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;}}}?>
在这个文件中,有三处需要改动:
第一:设置 TOKEN
define("TOKEN", "abcdefg123");//第一处修改
这里的数值,需要与前面网页中的 Token 的设置严格一致。
第二:这个范例程序中只是设置了验证模式,而没有消息的响应处理,笔者将该文件进行了修改,当存在验证参数时进行验证,而不存在验证信息时,则调用响应方法。
if ( $_GET["echostr"] ) { $wechatObj->valid(); } else { $wechatObj->responseMsg(); }
第三:在范例程序中,对输入信息进行了一点处理,设置为返回信息。
$contentStr = "Welcome to wechat world!".$keyword;
也就是将微信粉丝输入的信息,加上"Welcome to wechat world!"后返回。
另外需要注意的是该 PHP 文件的编码也必须是 UTF-8 的。上传(push)PHP 文件,如果上传正确,则等待应用执行。
该文件名指定必须与前述的配置文件完全一致。
在正常的配置结束后,就可以在 Bluemix 中看到该应用,该应用处于正常运行的状态。
图 9
cf api https://api.ng.bluemix.net cf login
此处输入申请的用户邮件、密码,执行至运行结束。
cf push -f manifest.yml
在微信公众平台中,点击提交,系统会提示验证通过。如果提示验证不通过,需要检查 URL、Token、程序中的 Token 以及程序代码的正确性。
另外,需要注意的是,腾讯要求服务器响应必须在 5 秒以内,因此在网络环境不够好的情况下,也许需要多提交几次才可以完成。
特别需要注意的是,笔者曾经进行过新浪的微博开发,新浪微博开发可以采取本地服务器,一样可以验证通过,而腾讯的这种验证必须使用外部可以访问的网址,笔者曾经为此很困扰,而且腾讯的验证必须使用 80 端口,不支持 URL 带端口号,这也曾经困扰过笔者。幸而 IBM 的 Bluemix 平台可以支持。
利用手机微信,向微信公众平台发送信息,获得微信公众平台返回的信息。
设置成功后,例如手机微信关注该微信公众平台账号,例如笔者的"正海说知识创新",账号:"zhszscx"关注的方法可以是名称查询、代号查询,也可以扫描如下的二维码,进行体验。
想该账号发送:hello!
将会收到系统返回的:Welcome to wechat world! hello!
这就表示基于 Bluemix 的微信公众平台开发顺利完成。
开发中注意事项
对于前面开发中介绍到的可能出现问题的地方再做一些总结:
正确设置 HOST 名称,不可以重复。
HOST 名称加上域名,需要和微信公众平台中的 URL 相匹配。
程序中的 Token 的设置必须同微信公众平台中的设置相匹配。
配置文件必须使用 UTF-8 格式。
Index.php 程序文件格式必须使用 UTF-8 格式。
系统未做说明,但是默认采用的是 index.php 文件作为入口,也就是说,如果按本文所叙述的程序编制方法,利用域名访问时,将没有结果展示。
在微信公众平台提交时,可能显示失败,需要提交多次,知道显示成功。
另外需要说明的是,目前的程序只对文本型微信信息进行了回复,其他信息未做处理。而开发者如果在开发中需要变更程序,则需要再次 PUSH 就可以,但是不需要在微信编辑模式再次提交。
发展展望
笔者在前述的基础上,又增加了数据库的链接,主要是 MYSQL 服务的建立、MYSQL 服务与应用的绑定,在 PHP 程序文件中实现服务的参数调用,数据表的创建、插入、显示,成功实现了数据库的处理,另外也成功部署了基于 PHP 的网站形式的应用,有了这样的基础,说明可以利用 IBM 的 Bluemix 平台实现开发微信公共账号应用。
The above is the detailed content of Detailed example of WeChat development account based on IBM Bluemix. For more information, please follow other related articles on the PHP Chinese website!