实践基于REST风格的Webservice(PHP,C#)

简介:


概念:

WEB服务的风格,从维基百科上查了一下,有不下十几种,但是比较常用的就是REST和RPC。其中,基于SOAP协议的Webservice就是RPC风格的。

REST全称Representational State Transfer。它是基于无状态的,cs结构的,可以缓存的通信协议。事实上,它是使用 HTTP协议的。某些观点来看,互联网本身就是基于HTTP的,因此也可以认为是一种基于REST风格的Webservice。REST风格的Webservice对于SOAP,CORBA(一种和SOAP互相竞争的协议)来说,属于轻量级。请求较之于也比较简单。比如,用SOAP的Webservice的请求可能是如下这样的一个XML,有用的信息都被包括在冗余的信息中:

<?xml version="1.0"?>

<soap:Envelope

xmlns:soap="http://www.w3.org/2001/12/soap-envelope"

soap:encodingStyle="http://www.w3.org/2001/12/soap-encoding">

<soap:body pb="http://www.acme.com/phonebook">

 <pb:GetUserDetails>

  <pb:UserID>12345</pb:UserID>

 </pb:GetUserDetails>

</soap:Body>

</soap:Envelope>

 

而使用REST的请求就很简单,只是一个URL地址:http://www.acme.com/phonebook/UserDetails/12345,只需要使用浏览器就能验证这个REST的Webservice是否正常。HTTP的请求方式有多种,GET,POST,PUT,PATCH,DELETE。REST同样可以利用这些请求方式。REST的Webservice的响应通常是一个XML文件,但是和SOAP不同的是,REST并不一定需要响应返回XML文件,它也可以是CSV格式或者Json格式。

很多公司都采用REST风格的Webservice,比如 Google Glass API,Twitter,Yahoo,Flickr,Amazon等。比如Google有一个Webservice,Google Maps API,它提供2种输出格式,JSON和XML。地址分别是http://maps.googleapis.com/maps/api/geocode/json?parameters和http://maps.googleapis.com/maps/api/geocode/xml?parameters

该服务的具体使用方法和参数满可以查阅https://developers.google.com/maps/documentation/geocoding/?hl=zh-CN&csw=1#XML

演示:

下面演示一个基于PHP语言开发的REST风格的webservice。

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
<?php
switch ( $_SERVER [ 'REQUEST_METHOD' ])
{
     case  'GET' :
         $id = $_GET [ "id" ];
         $arrayid  explode ( "," $id );
         search( $arrayid );
         break ;
     case  'POST' :
         $id = $_POST [ "id" ];
         $username = $_POST [ "username" ];
         $sex = $_POST [ "sex" ];
         $age = $_POST [ "age" ];
                                       
         add( $id , $username , $sex , $age );
         break ;
     default :
         exit ();
}
function  search( $arrayid )
{
     $users =getCsvFile( "example.csv" );
     $string = "" ;
     foreach ( $users  as  $a )
     {
         $id = $a [0];
         if (in_array( $id , $arrayid ))
         {
             $string .= <<<EOF
     <user id= "$a[0]" >
         <name> $a [1]</name>
         <sex> $a [2]</sex>
         <age> $a [3]</age>
     </user>
EOF;
         }
     }
     $string = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
             . "<data>\r\n"
             . $string
             . "</data>" ;
                                   
     header( "Content-type:application/xml" );
     print ( $string );
                                   
}
function  add( $id , $username , $sex , $age )
{
     $users =getCsvFile( "example.csv" );
     $string = "$id,$username,$sex,$age" ;
                                   
     WriteLinetxt( $string , "example.csv" );
                                   
     $string = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\r\n"
             . "<return>\r\n"
             . "  <status>0</status>\r\n"
             . "</return>\r\n" ;
                                   
     header( "Content-type:application/xml" );
     print ( $string );
                                   
}
function  getCsvFile( $filepath )
     {
         $handle =null;
         $returnvalue = array ();
         try
         {
             $handle  fopen ( $filepath , "r" );
             while  ( $data  fgetcsv ( $handle , 1000,  "," ))
             {
                 array_push ( $returnvalue , $data );
             }
             fclose( $handle );
         }
         catch (Exception  $e )
         {
             fclose( $handle );
             $handle =null;
             die ( "Error!: " . $e ->getMessage(). "<br/>" );
         }
                                       
         return  $returnvalue ;
     }
function  WriteLinetxt( $content , $filename )
{
         file_put_contents ( $filename $content . "\r\n" ,FILE_APPEND);
}
?>

代码说明:

上述代码,根据请求的方式是POST还是GET来区分,如果是GET的话,则需带上查询的ID。服务会返回一个Content-type为application/xml的一份xm文档。如果是POST的话,则会把相应的POST进来的值写入到数据库。

本例中,数据库只是简单的采用CSV文件,即用逗号分割数据的文本文件。数据文件如下:

CBD247FF7AF9473193DC06EB24DBCCC8

由于REST风格的web服务可以通过浏览器验证,因此可以输入url地址测试。本例中没有采用url重写,如果使用url重写的话,服务地址会更友好。

D87BE31CF24D416BA901BA11EF15A2A6

服务部署好之后,就可以调用了。在PHP中,可以通过simplexml_load_file方法,get到这个xml文件。

1
2
3
4
5
<?php
$url = "http://localhost:8080/b.php?id=1001,1003,1004" ;
$response =simplexml_load_file( $url );
var_dump( $response );
?>

74A2BDE9531F4A879C284A805D575126

通过var_dump,可以看到获得的直接是一个SimpleXMLElement对象,当然你也可以把它转换成string对象。

下面用php代码实现模拟POST数据。PHP常用的模拟POST动作的方法有3种,分别为Curl、socket、file_get_contents,这里还是采用file_get_contents方法。代码如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
<?php
$url = "http://localhost:8080/b.php" ;
$data  array
(
     'id'  =>  '1990' ,
     'username'  =>  '小张' ,
     'sex'  =>  '男' ,
'age' => '20'
);
$post_string =http_build_query( $data );
$context  array (
     'http'  =>  array (
         'method'  =>  'POST' ,
'header' => 'content-type: application/x-www-form-urlencoded' ,
         'content'  =>  $post_string )
     );
$stream_context  = stream_context_create( $context );
$response  file_get_contents ( $url , false,  $stream_context );
$xml =simplexml_load_string( $response );
var_dump( $xml );
?>

代码中,模拟了POST提交,并且获得了返回的string,再把string转成了SimpleXMLElement对象。而且数据库中数据也已经写入。

5ABB6024DDF8491FAE0A051F8E524C15

C#版本访问:

C#也可以通过代码调用PHP写的REST风格的Webservice。代码如下:

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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
class  Program
    {
        static  void  Main( string [] args)
        {
            string [] paramName = {  "id"  };
            string [] paramVal = {  "1001,1004"  };
            var  p = HttpGet( "http://localhost:8080/b.php" , paramName, paramVal);
            Console.WriteLine(p);
            Console.WriteLine( "-------------------------------" );
            string [] paramName1 = {  "id" "username" "sex" "age"  };
            string [] paramVal1 = {  "1030" "tom" "男" "23"  };
            var  p1 = HttpPost( "http://localhost:8080/b.php" , paramName1, paramVal1);
            Console.WriteLine(p1);
        }
        static  string  HttpGet( string  url,  string [] paramName,  string [] paramVal)
        {
            StringBuilder paramz =  new  StringBuilder();
            for  ( int  i = 0; i < paramName.Length; i++)
            {
                paramz.Append(paramName[i]);
                paramz.Append( "=" );
                paramz.Append(HttpUtility.UrlEncode(paramVal[i]));
                paramz.Append( "&" );
            }
            url +=  "?"  + paramz.ToString();
            HttpWebRequest req = WebRequest.Create(url)  as  HttpWebRequest;
            string  result =  null ;
            using  (HttpWebResponse resp = req.GetResponse()  as  HttpWebResponse)
            {
                StreamReader reader =  new  StreamReader(resp.GetResponseStream());
                result = reader.ReadToEnd();
            }
            return  result;
        }
        static  string  HttpPost( string  url,  string [] paramName,  string [] paramVal)
        {
            HttpWebRequest req = WebRequest.Create( new  Uri(url))  as  HttpWebRequest;
            req.Method =  "POST" ;
            req.ContentType =  "application/x-www-form-urlencoded" ;
            // Build a string with all the params, properly encoded.
            // We assume that the arrays paramName and paramVal are
            // of equal length:
            StringBuilder paramz =  new  StringBuilder();
            for  ( int  i = 0; i < paramName.Length; i++)
            {
                paramz.Append(paramName[i]);
                paramz.Append( "=" );
                paramz.Append(HttpUtility.UrlEncode(paramVal[i]));
                paramz.Append( "&" );
            }
            // Encode the parameters as form data:
            byte [] formData = UTF8Encoding.UTF8.GetBytes(paramz.ToString());
            req.ContentLength = formData.Length;
            // Send the request:
            using  (Stream post = req.GetRequestStream())
            {
                post.Write(formData, 0, formData.Length);
            }
            // Pick up the response:
            string  result =  null ;
            using  (HttpWebResponse resp = req.GetResponse()  as  HttpWebResponse)
            {
                StreamReader reader =  new  StreamReader(resp.GetResponseStream());
                result = reader.ReadToEnd();
            }
            return  result;
        }
    }

F8FED7F3BB7C4808801FE1FA3AC1C220

参考文档:

http://geeknizer.com/rest-vs-soap-using-http-choosing-the-right-webservice-protocol/

http://msdn.microsoft.com/zh-cn/magazine/dd942839.aspx

http://www.gen-x-design.com/archives/create-a-rest-api-with-php/

http://rest.elkstein.org/2008/02/what-is-rest.html

















本文转自cnn23711151CTO博客,原文链接: http://blog.51cto.com/cnn237111/1285298,如需转载请自行联系原作者



相关文章
|
28天前
|
程序员 PHP
PHP程序员的成长之路:技术探索与实践
在当今数字化时代,PHP作为一种广泛应用的后端编程语言,对于程序员而言具有重要意义。本文从技术探索和实践的角度出发,探讨了PHP程序员在成长过程中所面临的挑战与机遇,以及如何通过持续学习与实践不断提升自身技能。
|
17天前
|
PHP
PHP 7.4的新特性及实践应用
【4月更文挑战第2天】本文主要介绍了PHP 7.4的新特性,并通过实例代码展示了如何在实际项目中应用这些新特性。文章首先简要回顾了PHP的发展历史,然后详细分析了PHP 7.4的新特性,包括预加载、数组解构、扩展的返回类型声明等。接下来,通过实际代码示例,演示了如何在项目中使用这些新特性。最后,总结了PHP 7.4新特性的优势和局限性,并展望了PHP未来的发展趋势。
|
29天前
|
安全 大数据 编译器
深入理解PHP 8.0的新特性及实践应用
【2月更文挑战第30天】随着PHP 8.0的发布,这一流行的服务器端脚本语言带来了许多令人兴奋的新特性和性能改进。本文将深入探讨PHP 8.0的关键新特性,包括JIT编译器、联合类型、名称参数、匹配表达式等,并通过实际代码示例展示如何利用这些新工具来编写更加高效、简洁和健壮的应用程序。无论您是PHP开发者还是对最新技术趋势感兴趣的技术爱好者,本文都将为您提供宝贵的信息和启发。
15 3
|
1月前
|
PHP 开发者
PHP中的命名空间深入理解与实践
【2月更文挑战第27天】在现代PHP开发中,命名空间是管理代码和避免名称冲突的重要工具。本文将深入探讨PHP命名空间的核心概念、实现原理及其在实际项目中的应用。通过分析命名空间的结构和使用场景,开发者可以更有效地组织代码,提升项目的可维护性和可扩展性。文章还将展示如何合理运用命名空间来优化代码设计,以及如何处理与自动加载机制的协同工作。
|
3月前
|
数据采集 存储 JavaScript
PHP编程实践:实际商品价格数据采集
PHP编程实践:实际商品价格数据采集
|
安全 PHP
Mac下使用pecl安装PHP的swoole扩展实践
我记得之前用brew安装的PHP,再使用pecl很方便就可以安装swoole,于是我把自带的PHP卸载然后重新使用brew安装,并且安装了pecl,在通过pecl安装了swoole扩展,把这个安装过程记录下来,方便下次使用吧
578 0
|
PHP
PHP数学函数实践四:将浮点数从零舍入到指定的小数位数
哈喽~今天我将给大家继续之前的PHP数学函数实践系列的文章,那么在上一篇《PHP数学函数实践三:随机函数rand()的巧妙运用》中给大家介绍了怎么使用rand()函数,感兴趣的朋友可以学习了解~ 本文带来PHP数学函数实践四!主要内容则是讲解如何将浮点数从零舍入到指定的小数位数? 首先给大家简单介绍下PHP中的浮点数是什么? 浮点型(也叫浮点数 float,双精度数 double 或实数 real)可以用以下任一语法定义:
186 0
|
Web App开发 应用服务中间件 PHP
|
PHP
使用PHP辅助快速制作一套自己的手写字体实践
笔者以前在网上看到有民间高手制作字体的相关事迹,觉得把自己的手写字用键盘敲出来是一件很有意思的事情,所以一直有时间想制作一套自己的手写体,前几天在网上搜索了一下制作字体的方法,发现技术上并不是太难,结合了自己PHP方面的开发经验,很快的做出了一套自己的手写字体。
12777 0