delphi 调用 php写的webservice
测试了一下delphi调用php写的webservice,代码如下:
第一步:使用php创建webservice,php创建webservice的两种方式,一种使用 WSDL 文件,一种不使用 WSDL
第二步:使用delphi连接php创建的webservice
具体步骤如下:
步骤一:
下载 SoapDiscovery.class.php 地址:http://www.phpclasses.org/browse/file/9476.html 源码:
<?php
/**
* Copyright (c) 2005, Braulio Jos� Solano Rojas
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without modification, are
* permitted provided that the following conditions are met:
*
* Redistributions of source code must retain the above copyright notice, this list of
* conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice, this list of
* conditions and the following disclaimer in the documentation and/or other materials
* provided with the distribution.
* Neither the name of the Solsoft de Costa Rica S.A. nor the names of its contributors may
* be used to endorse or promote products derived from this software without specific
* prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
* CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
* INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
* EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
*
* @version $Id$
* @copyright 2005
*/
/**
* SoapDiscovery Class that provides Web Service Definition Language (WSDL).
*
* @package SoapDiscovery
* @author Braulio Jos� Solano Rojas
* @copyright Copyright (c) 2005 Braulio Jos� Solano Rojas
* @version $Id$
* @access public
**/
class SoapDiscovery {
private $class_name = '';
private $service_name = '';
/**
* SoapDiscovery::__construct() SoapDiscovery class Constructor.
*
* @param string $class_name
* @param string $service_name
**/
public function __construct($class_name = '', $service_name = '') {
$this->class_name = $class_name;
$this->service_name = $service_name;
}
/**
* SoapDiscovery::getWSDL() Returns the WSDL of a class if the class is instantiable.
*
* @return string
**/
public function getWSDL() {
if (empty($this->service_name)) {
throw new Exception('No service name.');
}
$headerWSDL = "<?xml version=\"1.0\" ?>\n";
$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";
$headerWSDL.= "<types xmlns=\"http://schemas.xmlsoap.org/wsdl/\" />\n";
if (empty($this->class_name)) {
throw new Exception('No class name.');
}
$class = new ReflectionClass($this->class_name);
if (!$class->isInstantiable()) {
throw new Exception('Class is not instantiable.');
}
$methods = $class->getMethods();
$portTypeWSDL = "\n<!-- Ports -->\n".'<portType name="'.$this->service_name.'Port">';
$bindingWSDL = "\n<!-- SOAP Bindings -->\n".'<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";
$serviceWSDL = "\n<!-- Service (location) -->\n".'<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";
$messageWSDL = "\n<!-- Messages -->\n";
foreach ($methods as $method) {
if ($method->isPublic() && !$method->isConstructor()) {
$portTypeWSDL.= '<operation name="'.$method->getName()."\">\n".'<input message="tns:'.$method->getName()."Request\" />\n<output message=\"tns:".$method->getName()."Response\" />\n</operation>\n";
$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";
$messageWSDL.= "<!-- Input parameters for method {$method->getName()} -->\n".'<message name="'.$method->getName()."Request\">\n";
$parameters = $method->getParameters();
foreach ($parameters as $parameter) {
if ($method->getDocComment()) {
$pattern = '/@param\s+(string|boolean|int|integer|float|double)/i';
preg_match($pattern, $method->getDocComment(), $matches);
$type = $matches[1];
}
else {
$type = 'string';
}
$messageWSDL.= '<part name="'.$parameter->getName()."\" type=\"xsd:{$type}\" />\n";
}
$messageWSDL.= "</message>\n";
if ($method->getDocComment()) {
$pattern = '/@return\s+(string|boolean|int|integer|float|double)/i';
preg_match($pattern, $method->getDocComment(), $matches);
$return = $matches[1];
}
else {
$return = 'string';
}
$messageWSDL.= "<!-- Output for method {$method->getName()} -->\n".'<message name="'.$method->getName()."Response\">\n";
$messageWSDL.= '<part name="'.$method->getName()."\" type=\"xsd:{$return}\" />\n";
$messageWSDL.= "</message>\n";
}
}
$portTypeWSDL.= "</portType>\n";
$bindingWSDL.= "</binding>\n";
return sprintf('%s%s%s%s%s%s', $headerWSDL, $messageWSDL, $portTypeWSDL, $bindingWSDL, $serviceWSDL, '</definitions>');
}
/**
* SoapDiscovery::getDiscovery() Returns discovery of WSDL.
*
* @return string
**/
public function getDiscovery() {
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>";
}
}
?>
步骤二:使用php创建一个类,用来处理客户端请求,文件名:HelloWorld.class.php
源码:
1 <?php 2 3 /** 4 * 5 * Copyright (c) 2005, Braulio Jos� Solano Rojas 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without modification, are 9 * permitted provided that the following conditions are met: 10 * 11 * Redistributions of source code must retain the above copyright notice, this list of 12 * conditions and the following disclaimer. 13 * Redistributions in binary form must reproduce the above copyright notice, this list of 14 * conditions and the following disclaimer in the documentation and/or other materials 15 * provided with the distribution. 16 * Neither the name of the Solsoft de Costa Rica S.A. nor the names of its contributors may 17 * be used to endorse or promote products derived from this software without specific 18 * prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 21 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 22 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 23 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 24 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 25 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 31 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 32 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 * 34 * 35 * @version $Id$ 36 * @copyright 2005 37 */ 38 39 40 /** 41 * HelloWorld Class that implements the typical first example of programming in every language. 42 * 43 * @package SoapDiscovery 44 * @author Braulio Jos� Solano Rojas 45 * @copyright Copyright (c) 2005 Braulio Jos� Solano Rojas 46 * @version $Id$ 47 * @access public 48 **/ 49 class HelloWorld { 50 private $nombre = ''; 51 52 /** 53 * HelloWorld::__construct() HelloWorld class Constructor. 54 * 55 * @param string $nombre 56 * @return string 57 **/ 58 public function __construct($name = 'World') { 59 $this->name = $name; 60 } 61 62 /** 63 * HelloWorld::greet() Greets the World xor $this->name xor $name if $name is not empty. 64 * 65 * @param string $nombre 66 * @return string 67 **/ 68 public function greet($name = '') { 69 $name = $name?$name:$this->name; 70 return 'Hello '.$name.'.'; 71 } 72 73 /** 74 * HelloWorld::servidorEstampillaDeTiempo() Returns server timestamp. 75 * 76 * @return int 77 **/ 78 public function serverTimestamp() { 79 return time(); 80 } 81 } 82 83 ?>
步骤三:创建webservice服务器文件,处理webservice,文件名:HelloWorldService.php,访问地址:http://test/webservice/HelloWorldService.php
客户端访问添加:?wsdl ,即使:http://test/webservice/HelloWorldService.php?wsdl
源码:
1 <?php 2 3 /** 4 * 5 * Copyright (c) 2005, Braulio Jos� Solano Rojas 6 * All rights reserved. 7 * 8 * Redistribution and use in source and binary forms, with or without modification, are 9 * permitted provided that the following conditions are met: 10 * 11 * Redistributions of source code must retain the above copyright notice, this list of 12 * conditions and the following disclaimer. 13 * Redistributions in binary form must reproduce the above copyright notice, this list of 14 * conditions and the following disclaimer in the documentation and/or other materials 15 * provided with the distribution. 16 * Neither the name of the Solsoft de Costa Rica S.A. nor the names of its contributors may 17 * be used to endorse or promote products derived from this software without specific 18 * prior written permission. 19 * 20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND 21 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, 22 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 23 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 24 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 25 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 26 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 27 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 30 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 31 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, 32 * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 33 * 34 * 35 * @version $Id$ 36 * @copyright 2005 37 */ 38 39 require_once 'HelloWorld.class.php'; 40 41 // Enciende el servidor o despliega WSDL 42 if (isset($_SERVER['REQUEST_METHOD']) && $_SERVER['REQUEST_METHOD']=='POST') { 43 $servidorSoap = new SoapServer('http://'.$_SERVER['SERVER_NAME'].':'.$_SERVER['SERVER_PORT'].$_SERVER['PHP_SELF'].'?wsdl'); 44 $servidorSoap->setClass('HelloWorld'); 45 $servidorSoap->handle(); 46 } 47 else { 48 require_once 'SoapDiscovery.class.php'; 49 50 // Crea el servidor de descubrimiento 51 $disco = new SoapDiscovery('HelloWorld','Solsoft_HelloWorld'); 52 header("Content-type: text/xml"); 53 if (isset($_SERVER['QUERY_STRING']) && strcasecmp($_SERVER['QUERY_STRING'],'wsdl')==0) { 54 $wsdl = $disco->getWSDL(); 55 file_put_contents('wsdl.xml',$wsdl); 56 echo $wsdl; 57 } 58 else { 59 echo $disco->getDiscovery(); 60 } 61 } 62 63 ?>
步骤四:使用php创建 SoapClient 测试刚刚创建的webservice ,文件名:client.php
1 <?php 2 ini_set('soap.wsdl_cache_enabled', "0"); //关闭wsdl缓存 3 4 $soap = new SoapClient('http://test/webservice/HelloWorldService.php?wsdl'); 5 6 echo $soap->__soapCall('greet',['rose']);
步骤五:上述代码测试通过后,使用delphi创建webservice测试
5.1:

5.2:

5.3:

5.4:

5.5:在创建的 HelloWorldService.pas文件中,添加 InvRegistry.RegisterInvokeOptions(TypeInfo(Solsoft_HelloWorldPort),ioDocument);//

5.6:在窗口中添加 THTTPRIO 组件,并修改组件的 url 为wsdl的url

5.7:测试代码


浙公网安备 33010602011771号