Tag Archive for 'REST'

REST开发的粗浅心得

公司的team blog还没有完全弄好,一些东西就先写在这里。

1 Zend的REST功能还是很强的,像我目前这样简单的开发基本上没有什么问题。
2 做到目前的应用背后的事情还真是不少,很多环节也还没有连接好,比如目前使用的车型数据库lite版本是基于sqlite的,本身这个sqlite文件是从sql server中生成的,而这又涉及到公司本身的一个车型数据库重构的问题。因为sqlite是一个单独文件,open api调用中不涉及到数据库的写入,因此是否可以通过类似于cdn或者缓存的方式进行分布,进行简单的负载均衡,还要很多试验,
3 对于php,trac,svn,都是第一次正儿八经的用,很多小问题会困扰一整天。
4 textmate是一个不错的开发工具,因为没有ide,所以性能各方面都没有任何问题,检查错误,直接运行,多文件编辑之类,很多功能的确对开发者来说很贴心。同事建议可以使用zend studio试一下,感觉是一个很复杂庞大的软件,有空再说了。
5 php是一个很灵活的语言,很多地方也很先进,比如我现在用到较多的对象类型,数组类型,都很灵活和方便,相比较delphi而言。不过因为delphi的ide功能太过于强大了,所以很多时候感觉php过于随意了。(当然这只是我初学者的看法而已。)倒是很想试试看delphi for php来进行开发,现在已经出到2.0了,价钱还是太贵。

接下来开始做局域网的测试,一些功能会用在sns中的webgame的开发。之后等到appkey完成后,进行公网测试。还有13万张图片的数据库如何优化处理,基于车型数据库lite的一些项目重构等等,挑战刚刚开始。我也算七八十岁学吹打。

相关内容

基于REST模式的车型参数显示demo

这个小小的例子是目前正在学习和开发的REST模式的一个实践,开发者通过REST方式,可以通过一个车型ID来显示一款车的车型参数,demo里面现在显示4个,目前lite版本的车型数据库可以显示18个参数。

1
2
3
4
5
6
7
8
9
10
11
require_once 'Zend/Rest/Client.php';
 
$client = new Zend_Rest_Client('http://127.0.0.1/~yijun/odb_rest_server.php');
 
echo '<br>';
$car = $client->GetCarDetail('7000','')->get();
echo $car->car_name.'<br>';
echo $car->car_factory.'<br>';
echo $car->car_brand.'<br>';
echo $car->car_series.'<br>';
echo '<br>';

结果是:
乐骋 1.4 SL手动型
上海通用汽车有限公司
雪佛兰(上海通用)
乐骋

当然这还只是一个例子,实际上开发者并不知道车型ID,还需要另外的查询函数。不管怎么样,艰难有趣的开放API之路走出了第一步。

相关内容

基于zend framework的REST测试

看着zend的教程,稍微修改了一下,达到了基本的效果,对于php还是初学,REST概念也比较陌生,实在是赶鸭子上架,没办法了。并且在leo的建议下,最后获得xml的呈现放在了ff下,原来在safari下效果一直有问题。

下面是通过基于REST的方法,从client调用server的函数,返回的xml的运行例子。

zend framework REST-1

下面是client的代码,基于zend的例子,不过有修改,后面的server端代码同样有修改。

1
2
3
4
5
6
7
8
9
10
<?php
/**
 * Connect to server and retrieve a greeting
 */
header("content-type: text/xml");
require_once 'Zend/Rest/Client.php';
 
$client = new Zend_Rest_Client('http://127.0.0.1/~yijun/Rest_Server.php');
 
echo $client->sayHello('Leo', 'Day')->get();

其中header这里是强制显示为xml,便于调试,实际应用中不需要。

server端代码如下,用两个方法,一个是sayHi,比较简单,返回一句话,另外一个就是上面的效果,sayHello返回一个程序处理过的xml。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
<?php
require_once 'Zend/Rest/Server.php';
 
/**
 * Say Hello
 *
 * @param string $who
 * @param string $when
 * @return SimpleXMLElement
 */
 
function sayHi($who, $when)
{
    return "Hello $who, Good $when";
}
 
function sayHello($who, $when)
{
     $xml ='<?xml version="1.0" encoding="ISO-8859-1"?><mysite><value>Hey '.$who.'! Hope you re having a good '.$when.'</value><code>200</code></mysite>';
 
	   // $xml = simplexml_load_string($xml);
	    return $xml;
}
 
$server = new Zend_Rest_Server();
$server->addFunction('sayHi');
$server->addFunction('sayHello');
 
$server->handle();

其中的simplexml这里我调试始终有问题,所以才在client这里强制用xml显示,可能php这里安装有问题。在server代码这里把这个转换屏蔽了,这里只是一个例子,实际应用还是需要打开的。在做后面真实应用的时候我会检查一下到底是什么原因导致这个函数失效。

相关内容

REST:我们未来的方向

说未来已经有点不准确了,实际上web2.0的重要特征之一就是开放API,而REST方式是开放API这个行为的实现模式之一。

Cakephp是这样介绍的:

Many newer application programmers are realizing the need to open their core functionality to a greater audience. Providing easy, unfettered access to your core API can help get your platform accepted, and allows for mashups and easy integration with other systems.

While other solutions exist, REST is a great way to provide easy access to the logic you’ve created in your application. It’s simple, usually XML-based (we’re talking simple XML, nothing like a SOAP envelope), and depends on HTTP headers for direction. Exposing an API via REST in CakePHP is simple.

在wikipedia上是这样解释的:

Representational state transfer (REST) is a style of software architecture for distributed hypermedia systems such as the World Wide Web. The terms “representational state transfer” and “REST” were introduced in 2000 in the doctoral dissertation of Roy Fielding,[1] one of the principal authors of the Hypertext Transfer Protocol (HTTP) specification. The terms have since come into widespread use in the networking community.

REST strictly refers to a collection of network architecture principles which outline how resources are defined and addressed. The term is often used in a looser sense to describe any simple interface which transmits domain-specific data over HTTP without an additional messaging layer such as SOAP or session tracking via HTTP cookies. These two meanings can conflict as well as overlap. It is possible to design any large software system in accordance with Fielding’s REST architectural style without using HTTP and without interacting with the World Wide Web.[citation needed] It is also possible to design simple XML+HTTP interfaces which do not conform to REST principles, and instead follow a model of remote procedure call. The difference between the uses of the term “REST” therefore causes some confusion in technical discussions.

Systems which follow Fielding’s REST principles are often referred to as “RESTful”.

在学习cakephp的时候,发现它对于REST模式的开发支持的也不错。稍后继续研究。

相关内容