设为首页 收藏本站
查看: 461|回复: 0

[经验分享] php5调用web service (笔者测试成功)

[复制链接]

尚未签到

发表于 2015-11-17 12:11:25 | 显示全部楼层 |阅读模式

工作中需要用php调用web service接口,对php不熟,上网搜搜,发现关于用php调用web service的文章也不多,不少还是php4里用nusoap这个模块调用的方法,其实php5里已经包含了处理soap的模块,但是资料太少了,上php官网上查帮助,写的不是很容易理解,经过多次实践,终于搞清楚了,php调用web service还是非常简单的。下面用一个例子说明:

  extension=php_openssl.dll
  extension=php_soap.dll
  php.ini 这2项要开通
  
web service服务是一个用java写的简单服务,环境为Tomcat6.0 + Axis2,暴露的方法为  String hello(String name)
传递一个String参数name,然后返回给客户端。
使用php5开发客户端:
<?php
header(&quot;content-type:text/html;charset=utf-8&quot;);
try {
    //$client = new SoapClient(&quot;HelloService.wsdl&quot;,array('encoding'=>'UTF-8'));
    $client = new SoapClient(&quot;http://localhost:8080/axis2/services/HelloService?wsdl&quot;,array('encoding'=>'UTF-8'));
  var_dump($client->__getFunctions());
  print(&quot;<br/>&quot;);
  var_dump($client->__getTypes());
  print(&quot;<br/>&quot;);
  
$parm1 = &quot;php client call&quot;;
$param = array('param0' => $parm1);
$arr = $client->hello($param);
print_r($arr);
} catch (SOAPFault $e) {
    print $e;
}
?>
代码确实很简单吧,创建SoapClient对象时,可以使用保存在本地WSDL文件,也可以使用远程的地址,后面的array数组里可以带上很多的参数,具体参数可以查php的SoapClient帮助,这里带的是字符集编码,如果调用方法的参数里有中文,一定要指定字符集编码,否则会出错。
调用web service前可以先调用SoapClient的__geunctions()和__getTypes()方法看一下你要调用的web service暴露的方法,参数和数据类型,需要注意的是传入的参数名一定要和soapclient里面定义的一致,否则参数是传不过去的。(本人在这个上面延误了很长时间c#调用不用指定参数名,真奇怪啊)
不知道WSDL的情况也可以调用WS,需要使用SoapClient的__soapCall()或__call()方法,具体使用方法可以查php的帮助文档。
另外发现个问题,如果web service方法返回的是xml&#26684;式的字符串,php接收到以后会自己把数据内容解析出来,而不是xml字符串.
  
  
有个叫Nusoap类,在php4下比较流行。但是淡水这次玩的是php5,所以他就没戏了。
先恶补一下相关知识。
先要打开php5的web service扩展。linux下,嗯,好像不会-_-!。windows下,把php.ini文件中 extension=php_soap.dll 去掉注释即可。
方法:
SoapClient->__soapCall()
说明:
class SoapClient {
mixed __soapCall ( string function_name, array arguments [, array options [, mixed input_headers [, array &output_headers]]])
}
In WSDL mode, you can simply call SOAP functions as SoapClient methods. This method useful in non-WSDL mode when soapaction is unknown, uri differs from the default or when sending and/or receiving SOAP Headers
返回&#20540;:
一个简单类型的返回&#20540;,或是一个关联数组
例子:

<?php

$client = new SoapClient(&quot;some.wsdl&quot;);
$client->SomeFunction($a, $b, $c);

$client->__soapCall(&quot;SomeFunction&quot;,
                     
array($a, $b, $c)
                     
);
$client->__soapCall(&quot;SomeFunction&quot;,
                    
array($a, $b, $c),
                    
NULL,
                    
new SoapHeader(),
                    
$output_headers
                    
);


$client = new SoapClient(null, array('location' =>&quot;http://localhost/soap.php&quot;,'uri'=> &quot;http://test-uri/&quot;));
$client->SomeFunction($a, $b, $c);
$client->__soapCall(&quot;SomeFunction&quot;,
                    
array($a, $b, $c)
                    
);
$client->__soapCall(&quot;SomeFunction&quot;,
                    
array($a, $b, $c),
                    
array('soapaction' => 'some_action','uri' => 'some_uri')
                    
);
?>1.in WSDL mode
soapCall应用web service,例子用的是asp.net的web service,提供service.asmx页面,调用及查看都比较简单,手册上的example也大多是这个类型,比较简单.

SOAP发送的协议:
POST /servicepath/service.asmx HTTP/1.1
Host: 211.186.1.4
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: “http://211.186.5.15/Service/ServiceMethod”

<?xml version=&quot;1.0&quot; encoding=&quot;utf-8&quot;?>
<soap:Envelope xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot;xmlns:soap=&quot;http://schemas.xmlsoap.org/soap/envelope/&quot;>
  
<soap:Body>
   
<ServiceMethod xmlns=&quot;http://211.186.5.15/Service&quot;>
      
<param1>string</param1>
      
<param2>string</param2>
      
<param3>string</param3>
   
</ServiceMethod>
  
</soap:Body>
</soap:Envelope>调用方法:
<?php
$client = new SoapClient(&quot;http://www.xxx.com/service/service.asmx?WSDL&quot;);
//向SOAP服务方发送参数&#20540;
$param1 = &quot;p1&quot;;
$param2 = &quot;p2&quot;;
$param3 = &quot;p3&quot;;

//serviceParam1,serviceParam2,serviceParam3为发送参数&#20540;所对应的参数名(或service端提供的字段名)
$param = array('serviceParam1' => $param1,'serviceParam2' =>$param2,'serviceParam3' => $param3);

//默认以parameters字段标示传递的参数数组(这里的web services是.net提供的,所以和php提供的Web service不同)
$arr = $client->__soapCall('ServiceMethod',array('parameters' => $param));
//这里淡水推荐直接使用web services提供的方法,如
//$arr = $client->ServiceMethod($param);

print_r($arr);
?>2.in non-WSDL mode
这种情况下soapaction is unknown

SOAP发送协议:
POST /services/SoapMethod?WSDL HTTP/1.1
Host: 220.211.1.12:8088
Connection: Keep-Alive
User-Agent: PHP-SOAP/5.2.5
Content-Type: text/xml; charset=utf-8
SOAPAction: “urn:SoapMethod#ServiceMethod”
Content-Length: 1297

<?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV=&quot;http://schemas.xmlsoap.org/soap/envelope/&quot;xmlns:ns1=&quot;urn:SoapMethod&quot; xmlns:xsd=&quot;http://www.w3.org/2001/XMLSchema&quot;xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot; xmlns:SOAP-ENC=&quot;http://schemas.xmlsoap.org/soap/encoding/&quot; xmlns:ns2=&quot;http://220.211.1.12&quot;SOAP-ENV:encodingStyle=&quot;http://schemas.xmlsoap.org/soap/encoding/&quot;>
<SOAP-ENV:Body>
<ns1:ServiceMethod>
<ServiceMethodSection xsi:type=&quot;ns2:ServiceObjectType&quot;>
<param1 xsi:type=&quot;xsd:string&quot;>01019</param1>
<param2 xsi:type=&quot;xsd:long&quot;>10</param2>
<param3 xsi:type=&quot;xsd:long&quot;>0</param3>
<param4 xsi:type=&quot;xsd:long&quot;>11</param4>
</ServiceMethodSection>
</ns1:ServiceMethod>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>调用方法:
<?php
//传递一个参数
try {
      
$client = new SoapClient(
                     
null,
                     
array('location' =>'http://192.168.1.180:8088/services/SoapPage?WSDL','uri' =>'http://192.168.1.180:8088/services/')
                     
);   
      
$result =  $client->__soapCall('ServiceMethod', array('fieldName' =>&quot;data&quot;)); //以数组形式传递params
      
//$result =  $client->__soapCall('ServiceMethod', array(new SoapParam(&quot;data&quot;, 'fieldName')));  //以构造服务端参数的形式构造参数传递给服务端  
      
var_dump($result);
  
}
  
catch (Exception $e)
  
{
      
printf(&quot;Message= %s&quot;,$e->__toString());
  
}
?>附注一下:
class SoapParam {
__construct ( mixed data, string name )
}
参数:
data
The data to pass or return. You can pass this parameter directly as PHP value, but in this case it will be named as paramN and the SOAP Service may not understand it.

name
参数名

<?php
//传递多个参数:
//如果服务端在non wsdl的情况下要求传递一个对象参数,该对象中包含多个属性,则

try {
      
$client = new SoapClient(
                     
null,
                     
array('location' =>'http://192.168.1.180:8088/services/SoapPage?WSDL','uri' => 'urn:SoapName')
                     
);//uri部分也可能是uri地址

      
class Obj{
      
public $param1 = '01019';
      
public $param2 = 10;
      
public $param3 = 0;
      
public $param4 = 11;
      
}

   
$struct = new Obj();  //创建服务端要求传递的对象
   
//如果服务端变态到传递的参数有的参数类型是你编程语言中没有的数据类型,
   
//如php,没有Java的Long类型(一般web service考虑通用性,数据类型都是string或int型)
   
//就要将参数进行强制类型转换
   
//这样的处理也只是在SOAP传输中将xml的参数类型替换成服务端所要求的类型名
   
//并不是真实转化传递的数据类型
   
$struct->param1 = iconv('gb2312','utf-8',$struct->param1);
   
$struct->param2 = new SoapVar($struct->param2,XSD_LONG);
   
$struct->param3 = new SoapVar($struct->param3,XSD_LONG);
   
$struct->param4 = new SoapVar($struct->param4,XSD_LONG);

   
//序列化对象中使用SoapVar的方法参考php手册对SoapVar的解释,每个参数都解释的很清楚
   
$soapstruct = new SoapVar($struct, SOAP_ENC_OBJECT, &quot;ServiceObjectType&quot;,&quot;http://soapinterop.org/xsd&quot;); //对象序列化,注意区分,SOAP对象的序列化不是用serialize
   
      
$result =  $client-> ServiceMethod(new SoapParam($soapstruct,'ServiceMethodSection'));
     
//$result =  $client->__soapCall('ServiceMethod', array(new SoapParam($soapstruct, 'ServiceMethodSection')));
     
      
var_dump($result);
  
} catch (Exception $e) {
      
printf(&quot;Message= %s&quot;,$e->__toString());
  
}
  ?>

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-140301-1-1.html 上篇帖子: nginx php5 mysql MyAdmin 下篇帖子: ubuntu下升级php5
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表