zoukankan      html  css  js  c++  java
  • php soap实例讲解

    一,什么是soap,什么是wsdl,为什么要用他们

    SOAP是基于XML和HTTP通信协议,xml各种平台,各种语言都支持的一个种语言。http呢它得到了所有的因特网浏览器及服务器的支持。

    WSDL 指网络服务描述语言 (Web Services Description Language),是一种使用 XML 编写的文档。这种文档可描述某个 Web service。它可规定服务的位置,以及此服务提供的操作。

    我是做php的,你是java的,他是做.net,如果我们三个之间要进行通信,要进行数据交换,怎么办呢?我们需要一个能和我们都能通信的工具。soap,wsdl被创造出来,使得运行在不同的操作系统并使用不同的技术和编程语言的应用程序可以互相进行通信。

    二,实例

    如果php要使用soap的话,通常做法是,添加了一下php的soap模块,在php.ini里面加上soap.so,下面介绍一个不要添加soap.so文件,也可以实现soap的方法

    nusoap是php写的一个功能文件,包涵进来就可以用了,网上很多自己去搜一下吧。

    1,不使用wsdl

    a,服务端helloworld2.php

    1. <?php  
    2. //包函nusoap.php  
    3. require_once('./lib/nusoap.php');  
    4.   
    5. //创建服务端  
    6. $server = new soap_server;  
    7.   
    8. //定义客户端调用方法  
    9. $server->register('hello');  
    10.   
    11. //调用方法以及参数  
    12. function hello($name) {  
    13.  return 'Hello, ' . $name;  
    14. }  
    15.   
    16. $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';  
    17. $server->service($HTTP_RAW_POST_DATA);  
    18. ?>  
    <?php//包函nusoap.phprequire_once('./lib/nusoap.php');//创建服务端$server = new soap_server;//定义客户端调用方法$server->register('hello');//调用方法以及参数function hello($name) { return 'Hello, ' . $name;}$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';$server->service($HTTP_RAW_POST_DATA);?>

    b,客户端hello.php

    1. <?php  
    2. //包函nusoap.php  
    3. require_once('./lib/nusoap.php');  
    4. //新建一个soap客户端,调用服务端提供的wsdl  
    5. //$client = new soapclient('http://localhost/test/hellowsdl2.php?wsdl', true);  
    6. $client = new soapclient('http://localhost/test/helloworld2.php');  
    7. //查看一下是不是报错  
    8. $err = $client->getError();  
    9. if ($err) {  
    10.  //显示错误  
    11.  echo '<h2>Constructor error</h2><pre>' . $err . '</pre>';  
    12. }  
    13.   
    14. //调用服务端的方法  
    15. $result = $client->call('hello'array('person' => "this is a test"));  
    16.   
    17. echo '<h2>Result</h2><pre>';  
    18. print_r($result);  
    19. echo '</pre>';  
    20. ?>  
    <?php//包函nusoap.phprequire_once('./lib/nusoap.php');//新建一个soap客户端,调用服务端提供的wsdl//$client = new soapclient('http://localhost/test/hellowsdl2.php?wsdl', true);$client = new soapclient('http://localhost/test/helloworld2.php');//查看一下是不是报错$err = $client->getError();if ($err) { //显示错误 echo '<h2>Constructor error</h2><pre>' . $err . '</pre>';}//调用服务端的方法$result = $client->call('hello', array('person' => "this is a test"));echo '<h2>Result</h2><pre>';print_r($result);echo '</pre>';?>

    2,使用wsld

    a,服务器端

    1. <?php  
    2. //包函nusoap.php  
    3. require_once('./lib/nusoap.php');  
    4. //新建一个soap服务  
    5. $server = new soap_server();  
    6. //初始化支持wsdl  
    7. $server->configureWSDL('hellowsdl2''urn:hellowsdl2');  
    8. //定义数据结构来接收数据  
    9. $server->wsdl->addComplexType(  
    10.  'Person',  
    11.  'complexType',  
    12.  'struct',  
    13.  'all',  
    14.  '',  
    15.  array(  
    16.  'firstname' => array('name' => 'firstname''type' => 'xsd:string'),//后面的type定义数据的类型,这个是string  
    17.  'age' => array('name' => 'age''type' => 'xsd:int'),//后面的type定义数据的类型,这个是int  
    18.  'gender' => array('name' => 'gender''type' => 'xsd:string')//后面的type定义数据的类型,这个是string  
    19.  )  
    20. );  
    21. $server->wsdl->addComplexType(  
    22.  'SweepstakesGreeting',  
    23.  'complexType',  
    24.  'struct',  
    25.  'all',  
    26.  '',  
    27.  array(  
    28.  'greeting' => array('name' => 'greeting''type' => 'xsd:string'),  
    29.  'winner' => array('name' => 'winner''type' => 'xsd:string')  
    30.  )  
    31. );  
    32. //服务器定义的soap调用方法  
    33. $server->register('hello',                    // 方法名字hello,方法就在下面  
    34.  array('person' => 'tns:Person'),          // 客户端传来的变量  
    35.  array('return' => 'tns:SweepstakesGreeting'),    //返回参数  
    36.  'urn:hellowsdl2',                         // soap名  
    37.  'urn:hellowsdl2#hello',                   // soap的方法名  
    38.  'rpc',                                    // 使用的方式  
    39.  'encoded',                                // 编码  
    40.  'test'                                    // 存档  
    41. );  
    42. //定义上面注册过的函数hello  
    43. function hello($person) {  
    44.  $greeting = 'Hello, ' . $person['firstname'] .  
    45.  '. It is nice to meet a ' . $person['age'] .  
    46.  ' year old ' . $person['gender'] . '.';  
    47.   
    48.  $winner =  'Scott';  
    49. //要返回的数据  
    50.  return array(  
    51.  'greeting' => $greeting,  
    52.  'winner' => $winner  
    53.  );  
    54. }  
    55. // 请求时(试图)调用服务  
    56. $HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';  
    57. $server->service($HTTP_RAW_POST_DATA);  
    58. ?>  
    <?php//包函nusoap.phprequire_once('./lib/nusoap.php');//新建一个soap服务$server = new soap_server();//初始化支持wsdl$server->configureWSDL('hellowsdl2', 'urn:hellowsdl2');//定义数据结构来接收数据$server->wsdl->addComplexType( 'Person', 'complexType', 'struct', 'all', '', array( 'firstname' => array('name' => 'firstname', 'type' => 'xsd:string'),//后面的type定义数据的类型,这个是string 'age' => array('name' => 'age', 'type' => 'xsd:int'),//后面的type定义数据的类型,这个是int 'gender' => array('name' => 'gender', 'type' => 'xsd:string')//后面的type定义数据的类型,这个是string ));$server->wsdl->addComplexType( 'SweepstakesGreeting', 'complexType', 'struct', 'all', '', array( 'greeting' => array('name' => 'greeting', 'type' => 'xsd:string'), 'winner' => array('name' => 'winner', 'type' => 'xsd:string') ));//服务器定义的soap调用方法$server->register('hello',                    // 方法名字hello,方法就在下面 array('person' => 'tns:Person'),          // 客户端传来的变量 array('return' => 'tns:SweepstakesGreeting'),    //返回参数 'urn:hellowsdl2',                         // soap名 'urn:hellowsdl2#hello',                   // soap的方法名 'rpc',                                    // 使用的方式 'encoded',                                // 编码 'test'                                    // 存档);//定义上面注册过的函数hellofunction hello($person) { $greeting = 'Hello, ' . $person['firstname'] . '. It is nice to meet a ' . $person['age'] . ' year old ' . $person['gender'] . '.'; $winner =  'Scott';//要返回的数据 return array( 'greeting' => $greeting, 'winner' => $winner );}// 请求时(试图)调用服务$HTTP_RAW_POST_DATA = isset($HTTP_RAW_POST_DATA) ? $HTTP_RAW_POST_DATA : '';$server->service($HTTP_RAW_POST_DATA);?>

    b,客户端

    1. <?php  
    2. //包函nusoap.php  
    3. require_once('./lib/nusoap.php');  
    4. //新建一个soap客户端,调用服务端提供的wsdl  
    5. //$client = new soapclient('http://localhost/test/hellowsdl2.php?wsdl', true);  
    6. $client = new soapclient('http://localhost/test/helloworld2.php');  
    7. //查看一下是不是报错  
    8. $err = $client->getError();  
    9. if ($err) {  
    10.  //显示错误  
    11.  echo '<h2>Constructor error</h2><pre>' . $err . '</pre>';  
    12. }  
    13. //要向服务端要传的参数  
    14. $person = array('firstname' => 'Willi''age' => 22, 'gender' => 'male');  
    15.   
    16. //调用服务端的方法  
    17. $result = $client->call('hello'array('person' => $person));  
    18. //错误审核  
    19. if ($client->fault) {  
    20.  echo '<h2>Fault</h2><pre>';  
    21.  print_r($result);  
    22.  echo '</pre>';  
    23. else {  
    24.  $err = $client->getError();  
    25.  if ($err) {  
    26.  echo '<h2>Error</h2><pre>' . $err . '</pre>';  
    27.  } else {  
    28.  echo '<h2>Result</h2><pre>';  
    29.  print_r($result);  
    30.  echo '</pre>';  
    31.  }  
    32. }  
    33. //显示请求信息  
    34. echo '<h2>Request</h2>';  
    35. echo '<pre>' . htmlspecialchars($client->request, ENT_QUOTES) . '</pre>';  
    36. //显示返回信息  
    37. echo '<h2>Response</h2>';  
    38. echo '<pre>' . htmlspecialchars($client->response, ENT_QUOTES) . '</pre>';  
    39. //显示调试信息  
    40. echo '<h2>Debug</h2>';  
    41. echo '<pre>' . htmlspecialchars($client->debug_str, ENT_QUOTES) . '</pre>';  
    42. ?>  
    <?php//包函nusoap.phprequire_once('./lib/nusoap.php');//新建一个soap客户端,调用服务端提供的wsdl//$client = new soapclient('http://localhost/test/hellowsdl2.php?wsdl', true);$client = new soapclient('http://localhost/test/helloworld2.php');//查看一下是不是报错$err = $client->getError();if ($err) { //显示错误 echo '<h2>Constructor error</h2><pre>' . $err . '</pre>';}//要向服务端要传的参数$person = array('firstname' => 'Willi', 'age' => 22, 'gender' => 'male');//调用服务端的方法$result = $client->call('hello', array('person' => $person));//错误审核if ($client->fault) { echo '<h2>Fault</h2><pre>'; print_r($result); echo '</pre>';} else { $err = $client->getError(); if ($err) { echo '<h2>Error</h2><pre>' . $err . '</pre>'; } else { echo '<h2>Result</h2><pre>'; print_r($result); echo '</pre>'; }}//显示请求信息echo '<h2>Request</h2>';echo '<pre>' . htmlspecialchars($client->request, ENT_QUOTES) . '</pre>';//显示返回信息echo '<h2>Response</h2>';echo '<pre>' . htmlspecialchars($client->response, ENT_QUOTES) . '</pre>';//显示调试信息echo '<h2>Debug</h2>';echo '<pre>' . htmlspecialchars($client->debug_str, ENT_QUOTES) . '</pre>';?>

    上面二个例子不管是客户端,还是服务器端,都是用php写的,你可以试着用多种语言来写,来测试一下。不管你是用php的模块,还是用nusoap,里面具体方法就不在这多说了,手册里面都有。

    SOAP在这里就不用介绍了,  这里只是简单的实现一个SOAP的实例, 不多说 ,看代码吧。 soap分为server和client, 我们要使client去调用server的代码. 首先看server短的代码:

        这个是server端的代码: server.php

        <?php

        //声明一个函数add() ,并返回它的值
            function add($a,$b){
            return $a+$b;
            }

        //实例化一个SoapServer对象, 并将add函数注册成为其方法
            $server = new SoapServer(null,array('uri'=>'http://localhost/')); //指定server端代码的URI(资源标志符)
            $server->addFunction("add");
            $server->handle();
        ?>

    然后使用client端的代码来调用server端的代码: client的代码也很简单: 如下:

        这个是client端的代码 client.php

        <?php

        //建立一个参数数组,存储要访问的提供soap服务的计算机的地址与程序
            $arrOptions=array(
                'uri'=>'http://localhost/',
                'location'=>'http://localhost/soap/server.php',  //注意: 这个location指定的是server端代码在服务器中的具体位置, 我的是在本地根目录下的soap目录中,
                'trace'=>true,
            );
            $soapObject = new SoapClient(null,$arrOptions); //实例化客户端对象
            echo $soapObject->add(20,30); //调用服务器端的函数add并返回值50
        ?>

    ok, 结束了 !

    aliyun活动 https://www.aliyun.com/acts/limit-buy?userCode=re2o7acl
  • 相关阅读:
    【BZOJ1486】最小圈(分数规划)
    【BZOJ4753】最佳团体(分数规划,动态规划)
    【POJ3621】【洛谷2868】Sightseeing Cows(分数规划)
    【POJ2728】Desert King(分数规划)
    【POJ2976】Dropping Tests(分数规划)
    【BZOJ4520】K远点对(KD-Tree)
    【BZOJ3991】寻宝游戏(动态规划)
    【BZOJ4066】简单题(KD-Tree)
    KD-Tree
    【BZOJ2648】SJY摆棋子(KD-Tree)
  • 原文地址:https://www.cnblogs.com/wangbin/p/1848035.html
Copyright © 2011-2022 走看看