主要记录一下 PHP 如何操作 SOAP,关于基础知识可以通过 《SOAP 与 WSDL 详解》 了解,这里使用 soap 官方扩展来进行实现
php.ini 开启扩展
- extension=soap.so
- extension=openssl.so
- extension=curl.so
1 2 3 4 |
extension=soap.so extension=openssl.so extension=curl.so |
配置 soap 相关参数
- [soap]
- ; Enables or disables WSDL caching feature. 开启或禁用WSDL缓存特性
- soap.wsdl_cache_enabled=1
- ; Sets the directory name where SOAP extension will put cache files.设置SOAP扩展放置缓存文件的目录
- soap.wsdl_cache_dir="/tmp"
- ; (time to live) Sets the number of second while cached file will be used 设置缓存生效时间(秒)
- ; instead of original one.
- soap.wsdl_cache_ttl=86400
1 2 3 4 5 6 7 8 9 |
[soap] ;Enables ordisables WSDL caching feature. 开启或禁用 WSDL 缓存特性 soap.wsdl_cache_enabled=1 ;Sets the directory name where SOAP extension will put cache files. 设置 SOAP 扩展放置缓存文件的目录 soap.wsdl_cache_dir="/tmp" ;(time tolive)Sets the number of second whilecached file will be used 设置缓存生效时间(秒) ;instead of original one. soap.wsdl_cache_ttl=86400 |
作为给定 web Services 的客户端,两种操作形式
用来提供 Web services,两种操作形式
- <?php
- Class RobotInfo
- {
- public function sayHello(){
- return "Say Hello!";
- }
- }
- //wsdl方式
- //$s = new SoapServer('RobotInfo.wsdl');
- //在non-wsdl方式中option服务端的location是选择性的,可以不提供
- $s = new SoapServer(null, array("location" => "http://localhost/server.php", "uri" => "server.php"));
- $s->setClass("RobotInfo");
- $s->handle();
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<?php ClassRobotInfo { publicfunctionsayHello(){ return"Say Hello!"; } } //wsdl 方式 //$s = new SoapServer('RobotInfo.wsdl'); // 在 non-wsdl 方式中 option 服务端的 location 是选择性的,可以不提供 $s=newSoapServer(null,array("location"=>"http://localhost/server.php","uri"=>"server.php")); $s->setClass("RobotInfo"); $s->handle(); |
- <?php
- try {
- //wsdl方式
- //$soap = new SoapClient("http://localhost/RobotInfo.wsdl");
- //在non-wsdl方式中option location系必须提供的
- $soap = new SoapClient(null, array('location' => "http://localhost/server.php", 'uri' => 'server.php'));
- //两种调用方式:直接调用方法和用__soapCall简接调用
- $result = $soap->sayHello(); // $soap->__soapCall("sayHello", array());
- echo $result;
- } catch (SoapFault $e) {
- echo $e->getMessage();
- } catch (Exception $e) {
- echo $e->getMessage();
- }
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php try{ //wsdl 方式 //$soap = new SoapClient("http://localhost/RobotInfo.wsdl"); // 在 non-wsdl 方式中 option location 系必须提供的 $soap=newSoapClient(null,array('location'=>"http://localhost/server.php",'uri'=>'server.php')); // 两种调用方式: 直接调用方法和用__soapCall 简接调用 $result=$soap->sayHello();// $soap->__soapCall("sayHello", array()); echo$result; }catch(SoapFault$e){ echo$e->getMessage(); }catch(Exception$e){ echo$e->getMessage(); } |
来源: https://www.congcong.us/post/php_soap_server_client.html