当前位置:文档之家› 云平台云计算大作业

云平台云计算大作业

云计算大作业一、实验目的:此次大作业实现了Web网页云平台的部署,数据库的写操作,以及微信公众号的建立以及关联;2.通过实际的部署对云平台有实际深入的了解,体会云平台相对于传统平台的优势;3.培养建立相对复杂系统的能力;二、实验平台:云平台使用的是新浪云,因为使用初期是免费的便于开发。

Web工程用MyEclipse 14建立,数据库采用云平台上的公共Mysql数据库,虽然安全性和稳定性*,但是免费。

微信申请的公众号为订阅号。

本实验网页采用jsp Web工程,微信接口采用php。

三、实验过程:实验参考了大量的网上教程,期中一些为直接引用。

申请云平台:/创建新应用:“控制台”—>“SAE”-->“创建新应用”;依照提示创建新应用。

由于本实验网页采用jsp Web工程,微信接口采用php。

所以分别建立java 1.7 jvm的云平台和php应用,由于是轻应用,所以设置应如下图所示,否则免费额度一天就用光。

如此配置一天要100左右。

二级域名自定义,当然土豪可申请一级域名。

图3:Java 1.7 JVM的建立图4:PHP空应用的建立图5:控制台建立好的平台(域名显示)编辑和添加代码:点击“管理应用”(就是网站的名字),进入管理界面,点击左栏“应用”-->“代码管理”进入如下页面:(之前可能要点击“创建版本”,记不清了)总之,只要看到这样的界面,就点击“编辑代码”,上传你已经编辑好的php的zip包(jvm是war包,具体导出步骤见下文)。

Eclipse导出war包:“file”→”export”→”选择一war形式导出”。

点击上传代码包,选择pHp代码上传。

(java代码因为用带数据,所以开数据库之后才能知道接口修改代码,再上传。

)开启公共数据库:点击左栏“数据库与缓存服务”,选择共享性数据库,相关链接代码:// 使用主库写数据使用SaeUserInfo提供的静态方法获取应用的AccessKey和SecretKey String URL="jdbc:mysql:// ";String username=SaeUserInfo.getAccessKey();String password=SaeUserInfo.getSecretKey();String driver="com.mysql.jdbc.Driver";Class.forName(driver).newInstance();Connection con=DriverManager.getConnection(url,username,password);// 使用从库读数据String URL="jdbc:mysql:// ";// 通过SaeUserInfo提供的静态方法获取应用的access_key和secret_keyString Username=SaeUserInfo.getAccessKey();String Password=SaeUserInfo.getSecretKey();String Driver="com.mysql.jdbc.Driver";Class.forName(Driver).newInstance();Connection con=DriverManager.getConnection(URL,Username,Password);之后上传Java代码。

四、实验部分代码:<?php/*罗网计划*/header('Content-type:text');define("TOKEN", "weixin");$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, SORT_STRING);$tmpStr = implode($tmpArr);$tmpStr = sha1($tmpStr);if($tmpStr == $signature){echo $echoStr;exit;}}//响应消息public function responseMsg(){$postStr = $GLOBALS["HTTP_RAW_POST_DATA"];if (!empty($postStr)){$this->logger("R \r\n".$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;case "image":$result = $this->receiveImage($postObj);break;case "location":$result = $this->receiveLocation($postObj);break;case "voice":$result = $this->receiveVoice($postObj);break;case "video":case "shortvideo":$result = $this->receiveVideo($postObj);break;case "link":$result = $this->receiveLink($postObj);break;default:$result = "unknown msg type: ".$RX_TYPE;break;}$this->logger("T \r\n".$result);echo $result;}else {echo "";exit;}}//接收事件消息private function receiveEvent($object){$content = "";switch ($object->Event){case "subscribe":$content = "欢迎使用罗网微信号\n请回复以下关键字:文本表情单图文多图文音乐\n请按住说话或点击+ 再分别发送以下内容:语音图片小视频我的收藏位置";$content .= (!empty($object->EventKey))?("\n来自二维码场景".str_replace("qrscene_","",$object->EventKey)):"";$content .= "\n\n".'<a href="http://1.132********">罗网计划</a>';break;case "unsubscribe":$content = "取消关注";break;case "CLICK":switch ($object->EventKey){case "COMPANY":$content = array();$content[] = array("Title"=>"罗网计划", "Description"=>"","PicUrl"=>"/weixin/weather/icon/cartoon.jpg", "Url" =>"http://1.132********");break;default:$content = "点击菜单:".$object->EventKey;break;}break;case "VIEW":$content = "跳转链接 ".$object->EventKey;break;case "SCAN":$content = "扫描场景 ".$object->EventKey;break;case "LOCATION":$content = "上传位置:纬度".$object->Latitude.";经度".$object->Longitude;break;case "scancode_waitmsg":if ($object->ScanCodeInfo->ScanType == "qrcode"){$content = "扫码带提示:类型二维码结果:".$object->ScanCodeInfo->ScanResult;}else if ($object->ScanCodeInfo->ScanType == "barcode"){$codeinfo = explode(",",strval($object->ScanCodeInfo->ScanResult));$codeValue = $codeinfo[1];$content = "扫码带提示:类型条形码结果:".$codeValue;}else{$content = "扫码带提示:类型".$object->ScanCodeInfo->ScanType." 结果:".$object->ScanCodeInfo->ScanResult; }break;case "scancode_push":$content = "扫码推事件";break;case "pic_sysphoto":$content = "系统拍照";break;case "pic_weixin":$content = "相册发图:数量 ".$object->SendPicsInfo->Count;break;case "pic_photo_or_album":$content = "拍照或者相册:数量 ".$object->SendPicsInfo->Count; break;case "location_select":$content = "发送位置:标签 ".$object->SendLocationInfo->Label; break;default:$content = "receive a new event: ".$object->Event;break;}if(is_array($content)){$result = $this->transmitNews($object, $content);}else{$result = $this->transmitText($object, $content);}return $result;}//接收文本消息private function receiveText($object){$keyword = trim($object->Content);//多客服人工回复模式if (strstr($keyword, "请问在吗") || strstr($keyword, "在线客服")){$result = $this->transmitService($object);return $result;}//自动回复模式if (strstr($keyword, "文本")){$content = "这是个文本消息";}else if (strstr($keyword, "表情")){$content = "微笑:/::)\n乒乓:/:oo\n中国:".$this->bytes_to_emoji(0x1F1E8).$this->bytes_to_emoji(0x1F1F3)."\n仙人掌:".$this->bytes_to_emoji(0x1F335);}else if (strstr($keyword, "单图文")){$content = array();$content[] = array("Title"=>"单图文标题", "Description"=>"单图文内容", "PicUrl"=>"/weixin/weather/icon/cartoon.jpg", "Url" =>"/?u=txw1958");}else if (strstr($keyword, "图文") || strstr($keyword, "多图文")){$content = array();$content[] = array("Title"=>"多图文1标题", "Description"=>"", "PicUrl"=>"/weixin/weather/icon/cartoon.jpg", "Url" =>"/?u=txw1958");$content[] = array("Title"=>"多图文2标题", "Description"=>"", "PicUrl"=>"/wisegame/pic/item/f3529822720e0cf3ac9f1a da0846f21fbe09aaa3.jpg", "Url" =>"/?u=txw1958");$content[] = array("Title"=>"多图文3标题", "Description"=>"", "PicUrl"=>"/wisegame/pic/item/18cb0a46f21fbe090d338a cc6a600c338644adfd.jpg", "Url" =>"/?u=txw1958");}else if (strstr($keyword, "音乐")){$content = array();$content = array("Title"=>"最炫民族风", "Description"=>"歌手:凤凰传奇", "MusicUrl"=>"/zxmzf.mp3", "HQMusicUrl"=>"/zxmzf.mp3");}else{$content = date("Y-m-d H:i:s",time())."\n\n".'<a href="http://1.132********">罗网计划</a>';}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 receiveImage($object){$content = array("MediaId"=>$object->MediaId);$result = $this->transmitImage($object, $content);return $result;}//接收位置消息private function receiveLocation($object){$content = "你发送的是位置,经度为:".$object->Location_Y.";纬度为:".$object->Location_X.";缩放级别为:".$object->Scale.";位置为:".$object->Label; $result = $this->transmitText($object, $content);return $result;}//接收语音消息private function receiveVoice($object){if (isset($object->Recognition) && !empty($object->Recognition)){$content = "你刚才说的是:".$object->Recognition;$result = $this->transmitText($object, $content);}else{$content = array("MediaId"=>$object->MediaId);$result = $this->transmitVoice($object, $content);}return $result;}//接收视频消息private function receiveVideo($object){$content = "上传视频类型:".$object->MsgType;$result = $this->transmitText($object, $content);return $result;}//接收链接消息private function receiveLink($object){$content = "你发送的是链接,标题为:".$object->Title.";内容为:".$object->Description.";链接地址为:".$object->Url;$result = $this->transmitText($object, $content);return $result;}//回复文本消息private function transmitText($object, $content){if (!isset($content) || empty($content)){return "";}$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 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){if(!is_array($musicArray)){return "";}$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 transmitImage($object, $imageArray){$itemTpl = "<Image><MediaId><![CDATA[%s]]></MediaId></Image>";$item_str = sprintf($itemTpl, $imageArray['MediaId']);$xmlTpl = "<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[image]]></MsgType>$item_str</xml>";$result = sprintf($xmlTpl, $object->FromUserName, $object->ToUserName, time());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 transmitVideo($object, $videoArray){$itemTpl = "<Video><MediaId><![CDATA[%s]]></MediaId><ThumbMediaId><![CDATA[%s]]></ThumbMediaId><Title><![CDATA[%s]]></Title><Description><![CDATA[%s]]></Description></Video>";$item_str = sprintf($itemTpl, $videoArray['MediaId'], $videoArray['ThumbMediaId'], $videoArray['Title'], $videoArray['Description']);$xmlTpl = "<xml><ToUserName><![CDATA[%s]]></ToUserName><FromUserName><![CDATA[%s]]></FromUserName><CreateTime>%s</CreateTime><MsgType><![CDATA[video]]></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 relayPart3($url, $rawData){$headers = array("Content-Type: text/xml; charset=utf-8");$ch = curl_init();curl_setopt($ch, CURLOPT_URL, $url);curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);curl_setopt($ch, CURLOPT_POST, 1);curl_setopt($ch, CURLOPT_POSTFIELDS, $rawData);$output = curl_exec($ch);curl_close($ch);return $output;}//字节转Emoji表情function bytes_to_emoji($cp){if ($cp > 0x10000){ # 4 bytesreturn chr(0xF0 | (($cp & 0x1C0000) >> 18)).chr(0x80 | (($cp & 0x3F000) >> 12)).chr(0x80 | (($cp & 0xFC0) >> 6)).chr(0x80 | ($cp & 0x3F));}else if ($cp > 0x800){ # 3 bytesreturn chr(0xE0 | (($cp & 0xF000) >> 12)).chr(0x80 | (($cp & 0xFC0) >> 6)).chr(0x80 | ($cp & 0x3F));}else if ($cp > 0x80){ # 2 bytesreturn chr(0xC0 | (($cp & 0x7C0) >> 6)).chr(0x80 | ($cp & 0x3F)); }else{ # 1 bytereturn chr($cp);}}//日志记录private function logger($log_content){if(isset($_SERVER['HTTP_APPNAME'])){ //SAEsae_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 = 1000000;$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('Y-m-d H:i:s')." ".$log_content."\r\n", FILE_APPEND);}}}>五、网站效果:网站只实现了信息的录入数据库以及微信号的自动回复和链接到网页。

相关主题