如何轻松地从PHP使用Web服务

是否有可用于PHP的任何工具,可用于生成基于其WSDL消费Web服务的代码? 相当于在Visual Studio中单击“添加Web引用”的东西,或者是为Java执行相同操作的Eclipse插件。

我用wsdl2php取得了很大的成功。 它会自动为您的Web服务中使用的所有对象和方法创build包装类。

在PHP 5中,您可以使用WSDL上的SoapClient调用Web服务函数。 例如 :

$client = new SoapClient("some.wsdl"); 

而$ client现在是一个具有some.wsdl中定义的类方法的对象。 所以如果在WSDL中有一个叫做getTime的方法,那么你只需要调用:

 $result = $client->getTime(); 

结果会(显然)在$ resultvariables中。 您可以使用__getFunctions方法返回所有可用方法的列表。

我过去曾经使用过NuSOAP 。 我喜欢它,因为它只是一组可以包含的PHP文件。 Web服务器上没有任何安装,也没有configuration选项可以更改。 它也有WSDL支持,这是一个奖金。

那么,这些function是特定于您正在使用这些语言进行开发的工具。

如果(例如)您使用记事本编写代码,则不会有这些工具。 所以,也许你应该问你正在使用的工具的问题。

对于PHP: http : //webservices.xml.com/pub/a/ws/2004/03/24/phpws.html

本文将介绍如何使用PHP SoapClient调用api Web服务。

嗨,我从这个网站得到这个: http : //forums.asp.net/t/887892.aspx?Consume+an+ASP+NET+Web+Service+with+PHP

Web服务具有Add两个参数的方法:

 <?php $client = new SoapClient("http://localhost/csharp/web_service.asmx?wsdl"); print_r( $client->Add(array("a" => "5", "b" =>"2"))); ?> 

假设您提供了以下内容:

 <x:Envelope xmlns:x="http://schemas.xmlsoap.org/soap/envelope/" xmlns:int="http://thesite.com/"> <x:Header/> <x:Body> <int:authenticateLogin> <int:LoginId>12345</int:LoginId> </int:authenticateLogin> </x:Body> </x:Envelope> 

 <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"> <s:Body xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema"> <authenticateLoginResponse xmlns="http://thesite.com/"> <authenticateLoginResult> <RequestStatus>true</RequestStatus> <UserName>003p0000006XKX3AAO</UserName> <BearerToken>Abcdef1234567890</BearerToken> </authenticateLoginResult> </authenticateLoginResponse> </s:Body> </s:Envelope> 

假设访问http://thesite.com/表示WSDL地址是:; http : //thesite.com/PortalIntegratorService.svc?wsdl

 $client = new SoapClient('http://thesite.com/PortalIntegratorService.svc?wsdl'); $result = $client->authenticateLogin(array('LoginId' => 12345)); if (!empty($result->authenticateLoginResult->RequestStatus) && !empty($result->authenticateLoginResult->UserName)) { echo 'The username is: '.$result->authenticateLoginResult->UserName; } 

正如你所看到的,尽pipe可以改变LoginId的值,但在PHP代码中使用了XML中指定的项目。