Manyou应用开发进阶
出自Manyou开发者指南
目录 |
引言
在创建“Hello world”程序里,我们介绍了创建一个应用的基本步骤,下面我们将创建一个名为“茶花女”的应用,当您创建自己的程序的时候,它可以作为一个模板。
使用这个教程需要您有一个支持PHP的web服务器和PHP脚本可以访问的MySQL数据库。
规划
创建目录及相关文件
| flowers/ images/ flowers/1.jpg 2.jpg 3.jpg ... styles/default.css index.php send.php receive.php lib.php common.php |
配置MySQL数据库
- 这个例子需要你在服务器上创建一个PHP脚本可以访问的数据库;
- 新建一个数据库;
- 在这个数据库中新建一个名为“flowers”的数据表,表结构如下:
| CREATE TABLE `flowers` ( `id` int(11) NOT NULL auto_increment, `from` int(11) NOT NULL default '0', `to` int(11) NOT NULL default '0', `time` int(11) NOT NULL default '0', `flower` int(11) NOT NULL default '0', `message` varchar(140) character set utf8 NOT NULL, PRIMARY KEY (`id`), KEY `from` (`from`), KEY `to` (`to`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ; |
- 将以下代码添加到common.php中,并配置数据库连接:
| <?php // Manyou客户端库文件 require_once './manyou.php'; // 类库文件 require_once './lib.php'; $config = array(); // 请从http://uchome.developer.manyou.com 申请应用获取密钥 $config['api_key'] = 'YOUR_API_KEY'; $config['api_secret'] = 'YOUR_SECRET'; // 您的数据库设置 $config['db_host'] = 'localhost'; //数据库服务器地址 $config['db_user'] = 'root'; //数据库用户名 $config['db_pass'] = ''; //数据库密码 $config['db_name'] = 'flowers'; //数据库名称 // 您的应用的地址 $config['app_url'] = 'http://flower.manyou-apps.com/'; // 鲜花设置 $config['flowers'] = array( 1 => '牡丹', 2 => '玫瑰', 3 => '菊花', 4 => '月季', 5 => '荷花', 6 => '梅花', 7 => '杜鹃', 8 => '雪花', 9 => '窗花', 10 => '麻花' ); // 实例化Manyou类库 $my = new Manyou($config['api_key'], $config['api_secret']); // 检测用户是否已添加应用 $my->require_add(); // 获取登录用户id $uid = intval($my->api_client->user_getLoggedInUser()); // 获取好友信息 $friends = $my->api_client->friend_get(); ?> <style type="text/css"> <?php echo file_get_contents('styles/default.css'); ?> </style> <!-- 创建一个标准的 Manyou 页面头部 --> <div id="head"> <my:header>茶花女</my:header> </div> |
- 注意样式表必须包含到<style type="text/css">...</style>里,而不能采用链接外部样式表文件的方式。
使用MYML创建发送鲜花页面
- 将以下代码添加到index.php中:
| <?php require_once './common.php'; // 通用文件,包括配置、Manyou初始化等 ?> <!-- 创建一组标准的 Manyou 标签导航栏。 --> <my:tabs> <my:tab_item title="送花给他/她" href="index.php" selected="true" /> <my:tab_item title="送出的鲜花" href="send.php" /> <my:tab_item title="接收的鲜花" href="receive.php" /> </my:tabs> <div id="content"> <!-- MYML Editor 内容编辑器 --> <my:editor action="index.php" labelwidth="10" width="750"> <my:editor-custom label=""> <h2>选择鲜花</h2> <ul class="avatar_list"> <?php foreach ($config['flowers'] as $key => $flower) { echo '<li>'; echo '<img src="' . $config['app_url'] . 'images/flowers/' . $key . '.jpg" />'; echo '<p><input type="radio" value="' . $key . '" name="flower_id" class="avatar_radio" />'; echo '' . $flower . '</p>'; echo '</li>'; } ?> </ul> </my:editor-custom> <my:editor-custom label=""> <h2>选择好友</h2> <?php if(count($friends)) { ?> <ul class="avatar_list"> <?php foreach($friends as $friend) { echo '<li>'; echo '<my:profile-pic uid="'.$friend.'" size="square" linked="true" />'; echo '<p><input type="radio" value="'.$friend.'" name="friend_id" />'; echo '<my:name uid="'.$friend.'" /></p>'; echo '</li>'; } ?> </ul> <?php } else { ?> 您还没有好友,请先添加好友。 <?php } ?> </my:editor-custom> <my:editor-custom> <h2>赠言</h2> <p style="margin:8px 0;">写上您的祝福的话,140个字符,每个汉字占3个字符。</p> </my:editor-custom> <my:editor-textarea label="" name="message"/> <my:editor-buttonset> <my:editor-button value="发送鲜花"/> <my:editor-button value="重置"/> </my:editor-buttonset> </my:editor> </div> |
在这个页面里,主要用到了一些MYML标签,MYML是一套使得应用与UCenter Home能够在界面上紧密结合的标记语言,详细介绍请查看MYML。
发送鲜花处理
- 将以下代码添加到lib.php中:
| <?php // 创建数据库连接 function get_db_conn() { static $conn = null; if($conn) return $conn; global $config; $conn = mysql_connect($config['db_host'], $config['db_user'], $config['db_pass']); mysql_select_db($config['db_name'], $conn); if(mysql_get_server_info() > '4.1') { mysql_query('SET NAMES "utf8"', $conn); } return $conn; } // 发送鲜花 function do_send($from, $to, $flower, $message) { global $my; global $config; // 写数据库 $conn = get_db_conn(); $time = time(); $message = mysql_real_escape_string($message); $sql = "INSERT INTO `flowers` ( `id` , `from` , `to` , `time` , `flower` , `message` ) VALUES (NULL , $from, $to, $time, $flower, '$message');"; if(!mysql_query($sql, $conn)) { return false; } // 发送Feed $feed_message = '通过 <a href="index.php">茶花女</a> 给<my:name uid="' . $to . '" linked="true" />送了一支' . $config['flowers'][$flower] . '。'; $feed_body = '<div style="float:left;height:48px;width:48px;padding:0;">'; $feed_body .= '<img src="' . $config['app_url'] . 'images/flowers/' . $flower . '.jpg" border="0" />'; $feed_body .= '</div>'; $feed_body .= '<div style="margin:0pt 0pt 0pt 62px;padding:0pt;">'; $feed_body .= '<p><span style="color:#999;">赠言:</span>' . $message . '</p></div>'; $feed_body .= '<div style="clear:both;"></div>'; $title_template = '{actor} ' . $feed_message . ''; $title_data = ''; $body_template = ''; $body_data = ''; $body_general = $message; //$image_1 = ''; $image_1 = $config['app_url'] . 'images/flowers/' . $flower . '.jpg'; $image_1_link = 'index.php'; $image_2 = ''; $image_2_link = ''; $image_3 = ''; $image_3_link = ''; $image_4 = ''; $image_4_link = ''; $target_ids = $to; $my->api_client->feed_publishTemplatizedAction( $title_template, $title_data, $body_template, $body_data, $body_general, $image_1, $image_1_link, $image_2, $image_2_link, $image_3, $image_3_link, $image_4, $image_4_link, $target_ids ); // 发送通知 $notification_info = '通过 <a href="index.php" title="茶花女">茶花女</a> 给你送了一支' . $config['flowers'][$flower] . ',赶快去<a href="receive.php">看看</a>吧!'; $my->api_client->notification_send($to, $notification_info); // 设置接收者个人主页应用信息 $profile_str = '<my:subtitle seeallurl="index.php?action=receive">收到的鲜花</my:subtitle>'; if ($profile_data = get_flowers(null, $to, 0, 3)) { $profile_str .= '<ul>'; foreach($profile_data as $profile_item) { $profile_str .= '<li style="border-bottom:1px solid #ecf1f3;padding:5px 15px 5px 5px;">'; $profile_str .= '<div style="float:left;width:48px;padding:0;">'; $profile_str .= '<img src="' . $config['app_url'] . 'images/flowers/' . $profile_item['flower'] . '.jpg" border="0" />'; $profile_str .= '<p style="text-align:center;padding:3px;font-weight:bold;">' . $config['flowers'][$profile_item['flower']] . '</p>'; $profile_str .= '</div>'; $profile_str .= '<div style="margin:0pt 0pt 0pt 62px;padding:0pt;"><span style="color:#999;">好友:</span><my:name uid="' . $profile_item['from'] . '" linked="true" />'; $profile_str .= '<p><span style="color:#999;">时间:</span>' . date('Y-m-d H:i:s', $profile_item['time']) . '</p>'; $profile_str .= '<p><span style="color:#999;">赠言:</span>' . $profile_item['message'] . '</p></div>'; $profile_str .= '<div style="clear:both;"></div>'; $profile_str .= '</li>'; } $profile_str .= '</ul>'; } else { $profile_str .= '<p>还没有人送花哦。</p>'; } $profile_str .= '<my:profile-action url="index.php"> 茶花女 </my:profile-action>'; $my->api_client->profile_setMYML($profile_str, $to); return true; } // 消息框 function message_box($title, $content = '', $type = 'success') { $str = '<my:' . $type . '>'; $str .= " <my:message>$title</my:message>"; $str .= $content; $str .= '</my:' . $type . '>'; return $str; } |
- 以上代码中,发送鲜花主要分四步:
- 将数据插入到MySQL数据库中;
- 发送Feed,Feed是Manyou提供给应用开发者的API,通过Feed,可以将应用的最新动态发到用户的好友页面,详细使用方法请查看Feed.publishTemplatizedAction。
- 发送通知,通知同样是Manyou API,作用是向一个用户发送通知或请求。详细使用方法请查看notification.send。
- 设置接收者个人主页应用信息。
- 将以下代码添加到index.php中<div id="content">标签下:
| <?php if (isset($_POST['flower_id']) && isset($_POST['friend_id'])) { $from = $uid; $to = intval($_POST['friend_id']); $flower = intval($_POST['flower_id']); $message = $my->no_magic_quotes($_POST['flower_message']); if($from && $to) { if (count($message) > 140) { echo message_box('错误提示', '赠言不能大于140个字符。', 'error'); } else if(do_send($from, $to, $flower, $message)) { echo message_box('发送成功', '您的鲜花已送出。', 'success'); } else { echo message_box('发送失败', '请再试一次。', 'error'); } } } ?> |
- 以上是鲜花发送的流程处理。
送出的鲜花页面
- 将以下代码添加到lib.php中:
| // 从数据库读取鲜花信息 function get_flowers($from = null, $to = null, $offset = 0, $limit = 30) { $conn = get_db_conn(); if($from) { $sql = "SELECT * FROM `flowers` WHERE `from` = $from ORDER BY `time` DESC LIMIT $offset , $limit"; } elseif ($to) { $sql = "SELECT * FROM `flowers` WHERE `to` = $to ORDER BY `time` DESC LIMIT $offset , $limit"; } else { return false; } if(!$result = mysql_query($sql, $conn)) { return false; } $data = array(); while ($row = mysql_fetch_assoc($result)) { $data[] = $row; } mysql_free_result($result); return $data; } |
- 将以下代码添加到send.php中:
| <?php // 通用文件,包括配置、Manyou初始化等 require_once './common.php'; ?> <my:tabs> <my:tab_item title="送花给他/她" href="index.php" /> <my:tab_item title="送出的鲜花" href="send.php" selected="true" /> <my:tab_item title="接收的鲜花" href="receive.php" /> </my:tabs> <div id="content"> <?php if ($data = get_flowers($uid)) { echo '<h2>您送出的鲜花</h2>'; echo '<ul class="post_list">'; foreach ($data as $item) { echo '<li>'; echo '<div class="post_flower"><img src="' . $config['app_url'] . 'images/flowers/' . $item['flower'] . '.jpg" border="0" />'; echo '<p><strong>' . $config['flowers'][$item['flower']] . '</strong></p></div>'; echo '<div class="post_item"><span>接收人:</span><my:name uid="' . $item['to'] . '" linked="true" />'; echo '<p><span>时间:</span>' . date('Y-m-d H:i:s', $item['time']) . '</p>'; echo '<p><span>赠言:</span>' . $item['message'] . '</p></div>'; echo '<div style="clear:both;"></div>'; echo '</li>'; } echo '</ul>'; } else { echo message_box('友情提示', '您还没有送花给别人。', 'explanation'); } ?> </div> |
接收的鲜花页面
- 将以下代码添加到receive.php中:
| <?php // 通用文件,包括配置、Manyou初始化等 require_once './common.php'; ?> <my:tabs> <my:tab_item title="送花给他/她" href="index.php" /> <my:tab_item title="送出的鲜花" href="send.php" /> <my:tab_item title="接收的鲜花" href="receive.php" selected="true" /> </my:tabs> <div id="content"> <?php if ($data = get_flowers(null, $uid)) { echo '<h2>好友送给您的鲜花</h2>'; echo '<ul class="post_list">'; foreach ($data as $item) { echo '<li>'; echo '<div class="post_flower"><img src="' . $config['app_url'] . 'images/flowers/' . $item['flower'] . '.jpg" border="0" />'; echo '<p><strong>' . $config['flowers'][$item['flower']] . '</strong></p></div>'; echo '<div class="post_item"><span>赠送人:</span><my:name uid="' . $item['from'] . '" linked="true" />'; echo '<p><span>时间:</span>' . date('Y-m-d H:i:s', $item['time']) . '</p>'; echo '<p><span>赠言:</span>' . $item['message'] . '</p></div>'; echo '<div style="clear:both;"></div>'; echo '</li>'; } echo '</ul>'; } else { echo message_box('友情提示', '还没有人给你送花哦。', 'explanation'); } ?> </div> |
