Sina App Quick Guide 1
-
date post
13-Sep-2014 -
Category
Documents
-
view
25.446 -
download
26
description
Transcript of Sina App Quick Guide 1
T.SINA.COM.CN/EASY
2009.11,2010.1 updated
Sina App Engine Quick Guide
提要
• SAE First Look • SAE Service• A Simple Demo• Questions
SAE First Look
• Web 应用开发和运行平台
SAE First Look
• Video
• http://xhprof.tools.sinaapp.com/demo/
SAE Service
• 数据存储• 图像处理• Mysql 数据库• Memcache• fetch_url
数据存储
• SaeStorage class
• // 初始化• $s = new SaeStorage;• // 写入数据• $s->write( $domain , $filename , $content );• // 读取数据• $s->read( $domain , $filename );• // 获取 url• $s->getUrl( $domain , $filename );
图像处理
• SaeImage class
• // 初始化• $img = new SaeImage($img_bin_data);• // 缩放• $img->resize( $width , $height );• // 旋转• $img->rotate( 90 );• // 执行以上操作并输出• $img_data_png = $img->exec(‘jpg’);
Mysql
• SaeMysql class
• // 初始化• $mysql= new SaeMysql();• // 运行 sql• $mysql->runSql( $sql );• // 查询数据• $data_array = $mysql->getData( $sql );• // 错误捕捉• If( $mysql->errno() != 0 ) die( “Mysql error” . $mysql-
>error() );
Memcache
• Sae_memcache
• // 初始化• $mc = sae_memcache_init();• // 存数据• $mc->set ( $set , $get );• // 取数据• $mc->get( $key );
Fetch_url
• $f = new SaeFetchurl();
• $content = $f->fetch( ‘http://sina.cn’ );• if( !$content ) echo $f->errmsg();
A Simple Demo : Let’s try it
• 需求 : 图片收藏夹• 描述 : 用户提交图片链接后 , 将图片保存 , 并以缩图方
式列表
Simple Demo
<?php// 初始化并保存各服务需要的数据define( 'ST_DOMAIN' , ‘' );
?>
Simple Demo
• 创建表单
• <form action=“index.php” method=“post” >• <input type=“text” name=“pic_url” /><br/>• <input type=“submit” value=“save”>• </form>
Simple Demo
• 抓取文件
• $f = new SaeFetchurl();• if( !$img_data = $f->fetch($_REQUEST['pic_url']))• die( 'can\'t get contents of ' . $_REQUEST['pic_url'] .
' error : ' . $f->errmsg() );
Simple Demo
• 图片处理 : 缩图和格式转换
• $img = new SaeImage( $img_data );• $img_data = $img->exec('jpg');• $img->resize( 100 );• $small_data = $img->exec('jpg');
Simple Demo
• 保存图片到存储
• $name = time() ;• $s = SaeStorage();• if( !$s->write( ST_DOMAIN , 'photo_' . $name . '.jpg' ,
$img_data ) )• die( 'save img data error' );• else• $url = $s->getUrl( ST_DOMAIN , 'photo_' . $name . '.jpg'
);• // 缩图同样处理
Simple Demo
• 向 Mysql 中添加记录
• $sql = "INSERT INTO `pic` ( `url` , `small_url` , `timeline` ) VALUES ( '" . $mysql->escape( $url ) . "' , '" . $mysql->escape( $small_url ) . "' , NOW() )";
• $mysql->runSql( $sql ); • if( $mysql->errno() != 0 )• die( 'Mysql error ' . $mysql->error() );• echo '<div style="color:red;padding:5px"> 数据成功保存
</div>';
Simple Demo
• 添加展示页面
• $sql = "SELECT * FROM `pic` ORDER BY `id` DESC LIMIT 10";
• $data = $mysql->getData( $sql );?>
• <?php foreach( $data as $line ): ?>• <div ><a href="<?php echo $line['url'] ?>"
target="new"><img src="<?php echo $line['small_url'] ?>"></a></div>
• <?php endforeach; ?>
Simple Demo
• All in one• http://apidemo.sinaapp.com/code.txt
Simple Demo
• Visit it• http://apidemo.sinaapp.com/
Find more fun?
• 文档中心 - wiki.sae.sina.com.cn• 邮件列表 - lists.sae.sina.com.cn
Questions?