使用新浪云(SAE)作为微信公众号服务器首先需要有一个新浪微博账号,没有的话,
请先注册一个,然后开始下面的步骤:
1 点击下面的地址,使用新浪微博账号登录,申请新浪云服务器资源。
2 点击顶部左上角菜单"控制台",在下拉菜单中选择"云应用 SAE",进入下面的页面:
点击左下角"创建新应用"按钮,在弹出的"SAE提醒您"窗口中,点击"继续创建"。
3 在弹出的窗口中,填写域名,应用名称,以及开发语言等:
例如,二级域名:weixinkaifa 应用名称:微信公众号开发测试 开发语言:PHP 5.6,然后选择空应用,完成应用创建。
4 完成创建后,会自动跳转到应用列表,可以看到刚才创建的应用,如下图:
请记住"应用信息"栏中,"微信公众号开发测试"下面的链接,这里为"http://weixinkaifa.applinzi.com"
5 点击应用"微信公众号开发测试", 进入"应用主页", 在左侧菜单"数据库服务"的下拉菜单中选择"独享型MySQL"
如下图:
点击"确认初始化", 初始化完成后,再点击"销毁MySQL"按钮, 如下图:
这样先创建独享型MySQL,是因为第一次使用独享型MySQL,奖励200云豆,而再销毁是因为独享型云豆比较贵,销毁完独享型MySql后,再按步骤5,重新创建一次数据库,这一次不需要再销毁。
6 在左侧菜单"应用"的下拉菜单中选择"代码管理",如下图:
选择使用"SVN"进行代理管理,创建完成后,会看到如下界面:
到这里就完成了一个应用的创建,再次强调,请记住第4步中的URL,这个URL将作为我们自己的微信公众号服务器。
7 将下面的代码用utf-8格式保存,再压缩成index.zip文件,点击第6步图中的"上传代码",上传成功后,点击第6步
图中的"编辑代码",可以查看上传的代码。
声明:以下代码来自"方倍工作室"的博文,非本人原创,博文链接:
http://www.cnblogs.com/txw1958/p/wechat-tutorial.html
以下代码经本人验证过可用,如果以下代码不可用,可到上面的地址上下载完整源码包index.zip
<?php
/*
方倍工作室
http://www.cnblogs.com/txw1958/
CopyRight 2014 All Rights Reserved
*/
define("TOKEN", "weixinkaifa");
$wechatObj = new wechatCallbackapiTest();
if (!isset($_GET['echostr'])) {
$wechatObj->responseMsg();
}else{
$wechatObj->valid();
}
class wechatCallbackapiTest
{
//验证签名
public function valid()
{
$echoStr = $_GET["echostr"];
$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){
header('content-type:text');
echo $echoStr;
exit;
}
}
//响应消息
public function responseMsg()
{
$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];
if (!empty($postStr)){
$this->logger("R ".$postStr);
$postObj = simplexml_load_string($postStr, 'SimpleXMLElement', LIBXML_NOCDATA);
$RX_TYPE = trim($postObj->MsgType);
//消息类型分离
switch ($RX_TYPE)
{
case "event":
$result = $this->receiveEvent($postObj);
break;
case "text":
$result = $this->receiveText($postObj);
break;
default:
$result = "unknown msg type: ".$RX_TYPE;
break;
}
$this->logger("T ".$result);
echo $result;
}else {
echo "";
exit;
}
}
//接收事件消息
private function receiveEvent($object)
{
$content = "";
switch ($object->Event)
{
case "subscribe":
$content = "欢迎关注方倍工作室 ";
$content .= (!empty($object->EventKey))?("\n来自二维码场景 ".str_replace("qrscene_","",$object->EventKey)):"";
break;
case "unsubscribe":
$content = "取消关注";
break;
case "SCAN":
$content = "扫描场景 ".$object->EventKey;
break;
case "CLICK":
switch ($object->EventKey)
{
case "COMPANY":
$content = array();
$content[] = array("Title"=>"多图文1标题", "Description"=>"", "PicUrl"=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958");
break;
default:
$content = "点击菜单:".$object->EventKey;
break;
}
break;
case "LOCATION":
$content = "上传位置:纬度 ".$object->Latitude.";经度 ".$object->Longitude;
break;
case "VIEW":
$content = "跳转链接 ".$object->EventKey;
break;
case "MASSSENDJOBFINISH":
$content = "消息ID:".$object->MsgID.",结果:".$object->Status.",粉丝数:".$object->TotalCount.",过滤:".$object->FilterCount.",发送成功:".$object->SentCount.",发送失败:".$object->ErrorCount;
break;
default:
$content = "receive a new event: ".$object->Event;
break;
}
if(is_array($content)){
if (isset($content[0])){
$result = $this->transmitNews($object, $content);
}else if (isset($content['MusicUrl'])){
$result = $this->transmitMusic($object, $content);
}
}else{
$result = $this->transmitText($object, $content);
}
return $result;
}
//接收文本消息
private function receiveText($object)
{
$keyword = trim($object->Content);
//多客服人工回复模式
if (strstr($keyword, "您好") || strstr($keyword, "你好") || strstr($keyword, "在吗")){
$result = $this->transmitService($object);
}
//自动回复模式
else{
if (strstr($keyword, "文本")){
$content = "这是个文本消息";
}else if (strstr($keyword, "单图文")){
$content = array();
$content[] = array("Title"=>"单图文标题", "Description"=>"单图文内容", "PicUrl"=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958");
}else if (strstr($keyword, "图文") || strstr($keyword, "多图文")){
$content = array();
$content[] = array("Title"=>"多图文1标题", "Description"=>"", "PicUrl"=>"http://discuz.comli.com/weixin/weather/icon/cartoon.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958");
$content[] = array("Title"=>"多图文2标题", "Description"=>"", "PicUrl"=>"http://d.hiphotos.bdimg.com/wisegame/pic/item/f3529822720e0cf3ac9f1ada0846f21fbe09aaa3.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958");
$content[] = array("Title"=>"多图文3标题", "Description"=>"", "PicUrl"=>"http://g.hiphotos.bdimg.com/wisegame/pic/item/18cb0a46f21fbe090d338acc6a600c338644adfd.jpg", "Url" =>"http://m.cnblogs.com/?u=txw1958");
}else if (strstr($keyword, "音乐")){
$content = array();
$content = array("Title"=>"最炫民族风", "Description"=>"歌手:凤凰传奇", "MusicUrl"=>"http://121.199.4.61/music/zxmzf.mp3", "HQMusicUrl"=>"http://121.199.4.61/music/zxmzf.mp3");
}else{
$content = date("Y-m-d H:i:s",time())."\n".$object->FromUserName."\n技术支持 方倍工作室";
}
if(is_array($content)){
if (isset($content[0]['PicUrl'])){
$result = $this->transmitNews($object, $content);
}else if (isset($content['MusicUrl'])){
$result = $this->transmitMusic($object, $content);
}
}else{
$result = $this->transmitText($object, $content);
}
}
return $result;
}
//回复文本消息
private function transmitText($object, $content)
{
$xmlTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[text]]></MsgType>
<Content><![CDATA[%s]]></Content>
</xml>";
$result = sprintf($xmlTpl, $object->FromUserName, $object->ToUserName, time(), $content);
return $result;
}
//回复语音消息
private function transmitVoice($object, $voiceArray)
{
$itemTpl = "<Voice>
<MediaId><![CDATA[%s]]></MediaId>
</Voice>";
$item_str = sprintf($itemTpl, $voiceArray['MediaId']);
$xmlTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[voice]]></MsgType>
$item_str
</xml>";
$result = sprintf($xmlTpl, $object->FromUserName, $object->ToUserName, time());
return $result;
}
//回复图文消息
private function transmitNews($object, $newsArray)
{
if(!is_array($newsArray)){
return;
}
$itemTpl = " <item>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
<PicUrl><![CDATA[%s]]></PicUrl>
<Url><![CDATA[%s]]></Url>
</item>
";
$item_str = "";
foreach ($newsArray as $item){
$item_str .= sprintf($itemTpl, $item['Title'], $item['Description'], $item['PicUrl'], $item['Url']);
}
$xmlTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[news]]></MsgType>
<ArticleCount>%s</ArticleCount>
<Articles>
$item_str</Articles>
</xml>";
$result = sprintf($xmlTpl, $object->FromUserName, $object->ToUserName, time(), count($newsArray));
return $result;
}
//回复音乐消息
private function transmitMusic($object, $musicArray)
{
$itemTpl = "<Music>
<Title><![CDATA[%s]]></Title>
<Description><![CDATA[%s]]></Description>
<MusicUrl><![CDATA[%s]]></MusicUrl>
<HQMusicUrl><![CDATA[%s]]></HQMusicUrl>
</Music>";
$item_str = sprintf($itemTpl, $musicArray['Title'], $musicArray['Description'], $musicArray['MusicUrl'], $musicArray['HQMusicUrl']);
$xmlTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[music]]></MsgType>
$item_str
</xml>";
$result = sprintf($xmlTpl, $object->FromUserName, $object->ToUserName, time());
return $result;
}
//回复多客服消息
private function transmitService($object)
{
$xmlTpl = "<xml>
<ToUserName><![CDATA[%s]]></ToUserName>
<FromUserName><![CDATA[%s]]></FromUserName>
<CreateTime>%s</CreateTime>
<MsgType><![CDATA[transfer_customer_service]]></MsgType>
</xml>";
$result = sprintf($xmlTpl, $object->FromUserName, $object->ToUserName, time());
return $result;
}
//日志记录
private function logger($log_content)
{
if(isset($_SERVER['HTTP_APPNAME'])){ //SAE
sae_set_display_errors(false);
sae_debug($log_content);
sae_set_display_errors(true);
}else if($_SERVER['REMOTE_ADDR'] != "127.0.0.1"){ //LOCAL
$max_size = 10000;
$log_filename = "log.xml";
if(file_exists($log_filename) and (abs(filesize($log_filename)) > $max_size)){unlink($log_filename);}
file_put_contents($log_filename, date('H:i:s')." ".$log_content."\r\n", FILE_APPEND);
}
}
}
?>
8 进入微信公众平台地址: https://mp.weixin.qq.com, 在左侧菜单中选择"开发"->"基本配置",点击
"修改配置"按钮,在基本配置页面中填写URL, Token, EncodingAESKey和消息加解密方式。
URL: 填写第4步中的URL, 这里为"http://weixinkaifa.applinzi.com"
Token:在第7步的代码中第一行定义了全局变量TOKEN,这里Token要填的值要和代码中定义的值一致
define("TOKEN", "weixinkaifa");
EncodingAESKey: 随机生成,不需要填写
消息加解密方式:选择"明文模式"即可。
9 点击"提交"按钮,点击"确定"按钮,配置成功后,回到"基本配置"页面,点击"启用"按钮,启用成功后,配置就完成了。
测试:打开你的微信公众号,发送"?",看看会不会回复当前时间,如果回复,就说明成功了,以后就可以使用
"http://weixinkaifa.applinzi.com" 作为微信公众号服务器进行公众号开发和测试了。