当前位置:   article > 正文

PHP调用webservice接口笔记_php 调用webservice接口开发

php 调用webservice接口开发

1、【开启php_soap.dll扩展】

找到php.ini文件
;extension=php_soap.dll

删除掉";" ,重启apache服务器

 

2、【下载SoapDiscovery.class.php类

这个类库文件直接网上搜索就有了,代码如下:

  1. <pre name="code" class="php"><?php
  2. /**
  3. * Copyright (c) 2005, Braulio José Solano Rojas
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without modification, are
  7. * permitted provided that the following conditions are met:
  8. *
  9. * Redistributions of source code must retain the above copyright notice, this list of
  10. * conditions and the following disclaimer.
  11. * Redistributions in binary form must reproduce the above copyright notice, this list of
  12. * conditions and the following disclaimer in the documentation and/or other materials
  13. * provided with the distribution.
  14. * Neither the name of the Solsoft de Costa Rica S.A. nor the names of its contributors may
  15. * be used to endorse or promote products derived from this software without specific
  16. * prior written permission.
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
  19. * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
  20. * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  21. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
  23. * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  25. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  26. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  27. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  28. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  29. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  30. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. *
  33. * @version $Id$
  34. * @copyright 2005
  35. */
  36. /**
  37. * SoapDiscovery Class that provides Web Service Definition Language (WSDL).
  38. *
  39. * @package SoapDiscovery
  40. * @author Braulio José Solano Rojas
  41. * @copyright Copyright (c) 2005 Braulio José Solano Rojas
  42. * @version $Id$
  43. * @access public
  44. **/
  45. class SoapDiscovery {
  46. private $class_name = '';
  47. private $service_name = '';
  48. /**
  49. * SoapDiscovery::__construct() SoapDiscovery class Constructor.
  50. *
  51. * @param string $class_name
  52. * @param string $service_name
  53. **/
  54. public function __construct($class_name = '', $service_name = '') {
  55. $this->class_name = $class_name;
  56. $this->service_name = $service_name;
  57. }
  58. /**
  59. * SoapDiscovery::getWSDL() Returns the WSDL of a class if the class is instantiable.
  60. *
  61. * @return string
  62. **/
  63. public function getWSDL() {
  64. if (empty($this->service_name)) {
  65. throw new Exception('No service name.');
  66. }
  67. $headerWSDL = "<?xml version=\"1.0\" ?>\n";
  68. $headerWSDL.= "<definitions name=\"$this->service_name\" targetNamespace=\"urn:$this->service_name\" xmlns:wsdl=\"http://schemas.xmlsoap.org/wsdl/\" xmlns:soap=\"http://schemas.xmlsoap.org/wsdl/soap/\" xmlns:tns=\"urn:$this->service_name\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:SOAP-ENC=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns=\"http://schemas.xmlsoap.org/wsdl/\">\n";
  69. $headerWSDL.= "<types xmlns=\"http://schemas.xmlsoap.org/wsdl/\" />\n";
  70. if (empty($this->class_name)) {
  71. throw new Exception('No class name.');
  72. }
  73. $class = new ReflectionClass($this->class_name);
  74. if (!$class->isInstantiable()) {
  75. throw new Exception('Class is not instantiable.');
  76. }
  77. $methods = $class->getMethods();
  78. $portTypeWSDL = '<portType name="'.$this->service_name.'Port">';
  79. $bindingWSDL = '<binding name="'.$this->service_name.'Binding" type="tns:'.$this->service_name."Port\">\n<soap:binding style=\"rpc\" transport=\"http://schemas.xmlsoap.org/soap/http\" />\n";
  80. $serviceWSDL = '<service name="'.$this->service_name."\">\n<documentation />\n<port name=\"".$this->service_name.'Port" binding="tns:'.$this->service_name."Binding\"><soap:address location=\"http://".$_SERVER['SERVER_NAME'].':'.$_SERVER['SERVER_PORT'].$_SERVER['PHP_SELF']."\" />\n</port>\n</service>\n";
  81. $messageWSDL = '';
  82. foreach ($methods as $method) {
  83. if ($method->isPublic() && !$method->isConstructor()) {
  84. $portTypeWSDL.= '<operation name="'.$method->getName()."\">\n".'<input message="tns:'.$method->getName()."Request\" />\n<output message=\"tns:".$method->getName()."Response\" />\n</operation>\n";
  85. $bindingWSDL.= '<operation name="'.$method->getName()."\">\n".'<soap:operation soapAction="urn:'.$this->service_name.'#'.$this->class_name.'#'.$method->getName()."\" />\n<input><soap:body use=\"encoded\" namespace=\"urn:$this->service_name\" encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" />\n</input>\n<output>\n<soap:body use=\"encoded\" namespace=\"urn:$this->service_name\" encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" />\n</output>\n</operation>\n";
  86. $messageWSDL.= '<message name="'.$method->getName()."Request\">\n";
  87. $parameters = $method->getParameters();
  88. foreach ($parameters as $parameter) {
  89. $messageWSDL.= '<part name="'.$parameter->getName()."\" type=\"xsd:string\" />\n";
  90. }
  91. $messageWSDL.= "</message>\n";
  92. $messageWSDL.= '<message name="'.$method->getName()."Response\">\n";
  93. $messageWSDL.= '<part name="'.$method->getName()."\" type=\"xsd:string\" />\n";
  94. $messageWSDL.= "</message>\n";
  95. }
  96. }
  97. $portTypeWSDL.= "</portType>\n";
  98. $bindingWSDL.= "</binding>\n";
  99. // return sprintf('%s%s%s%s%s%s', $headerWSDL, $portTypeWSDL, $bindingWSDL, $serviceWSDL, $messageWSDL, '</definitions>');
  100. $fso = fopen($this->class_name . ".wsdl" , "w");
  101. fwrite($fso, sprintf('%s%s%s%s%s%s', $headerWSDL, $portTypeWSDL, $bindingWSDL, $serviceWSDL, $messageWSDL, '</definitions>'));
  102. }
  103. /**
  104. * SoapDiscovery::getDiscovery() Returns discovery of WSDL.
  105. *
  106. * @return string
  107. **/
  108. public function getDiscovery() {
  109. return "<?xml version=\"1.0\" ?>\n<disco:discovery xmlns:disco=\"http://schemas.xmlsoap.org/disco/\" xmlns:scl=\"http://schemas.xmlsoap.org/disco/scl/\">\n<scl:contractRef ref=\"http://".$_SERVER['SERVER_NAME'].':'.$_SERVER['SERVER_PORT'].$_SERVER['PHP_SELF']."?wsdl\" />\n</disco:discovery>";
  110. }
  111. }
  112. ?>

 

 

 

3、【生成wsdl文件】

创建文件server.php,代码如下:

  1. <?php
  2. define('WSDL_URL','Test.wsdl'); //定义WSDL文件路径
  3. ini_set('soap.wsdl_cache_enabled','0'); //关闭WSDL缓存
  4. //WSDL文件不存在时自动创建
  5. if(!file_exists(WSDL_URL))
  6. {
  7. require_once 'SoapDiscovery.class.php';
  8. $disco = new SoapDiscovery('Test','domainName');/* domainName为你的域名,可以随便写 */
  9. $disco->getWSDL();
  10. }
  11. //SOAP开启并接收Client传入的参数响应
  12. $server = new SoapServer(WSDL_URL);
  13. $server->setClass('Test');
  14. $server->handle();
  15. /* 测试类 */
  16. class Test {
  17. public function hello($name = '')
  18. {
  19. return 'Hello '.$name.'.';
  20. }
  21. }

运行server.php文件,即可生成Test.wsdl文件,如下:

  1. <?xml version="1.0" ?>
  2. <definitions name="domainName" targetNamespace="urn:domainName" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="urn:domainName" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns="http://schemas.xmlsoap.org/wsdl/">
  3. <types xmlns="http://schemas.xmlsoap.org/wsdl/" />
  4. <portType name="domainNamePort"><operation name="hello">
  5. <input message="tns:helloRequest" />
  6. <output message="tns:helloResponse" />
  7. </operation>
  8. </portType>
  9. <binding name="domainNameBinding" type="tns:domainNamePort">
  10. <soap:binding style="rpc" transport="http://schemas.xmlsoap.org/soap/http" />
  11. <operation name="hello">
  12. <soap:operation soapAction="urn:domainName#Test#hello" />
  13. <input><soap:body use="encoded" namespace="urn:domainName" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
  14. </input>
  15. <output>
  16. <soap:body use="encoded" namespace="urn:domainName" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
  17. </output>
  18. </operation>
  19. </binding>
  20. <service name="domainName">
  21. <documentation />
  22. <port name="domainNamePort" binding="tns:domainNameBinding"><soap:address location="http://localhost/server.php" />
  23. </port>
  24. </service>
  25. <message name="helloRequest">
  26. <part name="name" type="xsd:string" />
  27. </message>
  28. <message name="helloResponse">
  29. <part name="hello" type="xsd:string" />
  30. </message>
  31. </definitions>

Test.wsdl文件中比较关键的地方用颜色标出来了,建议大家自己生成下,不要直接复制。

 

4、【调用接口】

创建client.php,代码如下:

  1. <?php
  2. $client = new SoapClient("http://localhost/server.php?wsdl", array('trace' => 1));/* 创建soapclient对象 */
  3. try {
  4. $result = $client->hello('man');/* 调用方法 */
  5. var_dump($result);
  6. } catch (SoapFault $f){
  7. echo '__getFunctions = ';
  8. var_dump($client->__getFunctions());
  9. echo '<br/>-----------------------<br/>';
  10. echo '__getLastRequest = '.$client->__getLastRequest();
  11. echo '<br/>-----------------------<br/>';
  12. echo '__getLastResponse = '.$client->__getLastResponse();
  13. echo '<br/>-----------------------<br/>';
  14. echo "Error Message: {$f->getMessage()}";
  15. }
  16. ?>

完成。
 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小蓝xlanll/article/detail/65952
推荐阅读
  

闽ICP备14008679号