有些时候,我们的程序会作为客户端,和服务端的例如(erp或者某某平台)进行一个WebService调用。
模拟场景
服务器端使用.net C#来完成webService,下面是一个下例子:
1 |
http://192.0.1.111/WebServiceTest/Service.asmx |
打开页面我们看到了定义的方法
我们已HelloWord方法为例,HelloWorld方法会返回一个helloworld字符串。
点击HelloWorld 来到链接http://192.0.1.111/WebServiceTest/Service.asmx?op=HelloWorld
你会看到 返回的字符串
1 2 3 4 5 6 7 8 9 10 11 |
POST /WebServiceTest/Service.asmx HTTP/1.1 Host: 192.0.1.111 Content-Type: application/soap+xml; charset=utf-8 Content-Length: length <?xml version="1.0" encoding="utf-8"?> <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"> <soap12:Body> <HelloWorld xmlns="http://tempuri.org/" /> </soap12:Body> </soap12:Envelope> |
1 2 3 4 5 6 7 8 9 10 11 12 |
HTTP/1.1 200 OK Content-Type: application/soap+xml; charset=utf-8 Content-Length: length <?xml version="1.0" encoding="utf-8"?> <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"> <soap12:Body> <HelloWorldResponse xmlns="http://tempuri.org/"> <HelloWorldResult>string</HelloWorldResult> </HelloWorldResponse> </soap12:Body> </soap12:Envelope> |
原生代码使用soap来调用
首先你的php.ini里php_soap.dll要开启
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
<?php $client = new SoapClient("http://192.0.1.111/WebServiceTest/Service.asmx?WSDL"); //向SOAP服务方发送参数值 $param1 = "p1"; $param2 = "p2"; $param3 = "p3"; //serviceParam1,serviceParam2,serviceParam3为发送参数值所对应的参数名(或service端提供的字段名) $param = array('serviceParam1' => $param1,'serviceParam2' =>$param2,'serviceParam3' => $param3); //默认以parameters字段标示传递的参数数组(这里的web services是.net提供的,所以和php提供的Web service不同) //$arr = $client->__soapCall('ServiceMethod',array('parameters' => $param)); $arr = $client->__soapCall('HelloWorld',array()); //这里推荐直接调用web services里提供的方法,如 //$arr = $client->ServiceMethod($param); //ServiceMethod 就等于这里的 HelloWorld print_r($arr->HelloWorldResult); ?> |
注释的代码(是传参方式的,HelloWorld方法没有参数所以不需要使用)。
你会看到返回的 HelloWorld字符串
symfony2中超简单
在symfony2中就变得超级简单了只需要两行代码搞定,首先做一下准备工作:
安装Bundle:https://github.com/noiselabs/NoiselabsNuSOAPBundle
Bundle安装完成后,在你的controller中输入如下代码即可实现同样效果
1 2 3 |
$client = new \nusoap_client('http://192.0.1.111/WebServiceTest/Service.asmx?WSDL', true); $result = $client->call('HelloWorld', array()); die(var_dump($result['HelloWorldResult'])); |
当然了,你说这不是3行吗?好吧,die()方法我没算上。
传入参数
普通参数:.net 发布webservice如下
点击打开GetString方法看到如下
SOAP 1.2
以下是 SOAP 1.2 请求和响应示例。所显示的占位符需替换为实际值。
1 2 3 4 5 6 7 8 9 10 11 12 13 |
POST /WebServiceTest/Service.asmx HTTP/1.1 Host: 192.0.1.111 Content-Type: application/soap+xml; charset=utf-8 Content-Length: length <?xml version="1.0" encoding="utf-8"?> <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"> <soap12:Body> <GetString xmlns="http://tempuri.org/"> <str>string</str> </GetString> </soap12:Body> </soap12:Envelope> |
1 2 3 4 5 6 7 8 9 10 11 12 |
HTTP/1.1 200 OK Content-Type: application/soap+xml; charset=utf-8 Content-Length: length <?xml version="1.0" encoding="utf-8"?> <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"> <soap12:Body> <GetStringResponse xmlns="http://tempuri.org/"> <GetStringResult>string</GetStringResult> </GetStringResponse> </soap12:Body> </soap12:Envelope> |
我们已symfony为例传入参数代码如下:
1 2 3 |
$client = new \nusoap_client('http://192.0.1.111/WebServiceTest/Service.asmx?WSDL', true); $result = $client->call('GetString', array('str'=> '测试成功' )); die(var_dump($result)); |
传入数组:webservice方法为
GetArrayV2
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
POST /WebServiceTest/Service.asmx HTTP/1.1 Host: 192.0.1.111 Content-Type: application/soap+xml; charset=utf-8 Content-Length: length <?xml version="1.0" encoding="utf-8"?> <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"> <soap12:Body> <GetArrayV2 xmlns="http://tempuri.org/"> <aList> <string>string</string> <string>string</string> </aList> </GetArrayV2> </soap12:Body> </soap12:Envelope> |
1 2 3 4 5 6 7 8 9 10 11 12 |
HTTP/1.1 200 OK Content-Type: application/soap+xml; charset=utf-8 Content-Length: length <?xml version="1.0" encoding="utf-8"?> <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"> <soap12:Body> <GetArrayV2Response xmlns="http://tempuri.org/"> <GetArrayV2Result>string</GetArrayV2Result> </GetArrayV2Response> </soap12:Body> </soap12:Envelope> |
我们已symfony为例传入参数代码如下:
1 2 3 |
$client = new \nusoap_client('http://192.0.1.111/WebServiceTest/Service.asmx?WSDL', true); $result = $client->call('GetArrayV2', array('aList'=> array('string'=>array('test1','test2')))); die(var_dump($result)); |
传入多维数组:方法为
GetArrayV3
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
POST /WebServiceTest/Service.asmx HTTP/1.1 Host: 192.0.1.111 Content-Type: application/soap+xml; charset=utf-8 Content-Length: length <?xml version="1.0" encoding="utf-8"?> <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"> <soap12:Body> <GetArrayV3 xmlns="http://tempuri.org/"> <aList> <ArrayOfString> <string>string</string> <string>string</string> </ArrayOfString> <ArrayOfString> <string>string</string> <string>string</string> </ArrayOfString> </aList> </GetArrayV3> </soap12:Body> </soap12:Envelope> |
返回参数字符串拼接
1 2 3 4 5 6 7 8 9 10 11 12 |
HTTP/1.1 200 OK Content-Type: application/soap+xml; charset=utf-8 Content-Length: length <?xml version="1.0" encoding="utf-8"?> <soap12:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap12="http://www.w3.org/2003/05/soap-envelope"> <soap12:Body> <GetArrayV3Response xmlns="http://tempuri.org/"> <GetArrayV3Result>string</GetArrayV3Result> </GetArrayV3Response> </soap12:Body> </soap12:Envelope> |
我们已symfony为例传入参数代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 |
$client = new \nusoap_client('http://192.0.1.111/WebServiceTest/Service.asmx?WSDL', true); $result = $client->call('GetArrayV3', array( 'aList'=> array( 'ArrayOfString'=> array( array('string'=>array('ceshi1','ceshi2')), array('string'=>array('ceshi3','ceshi4')) ) ) )); die(var_dump($result)); |
有了这些就可以应对较复杂的参数传递了。