如何从 PHP 轻松使用 Web 服务

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4430/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 21:08:38  来源:igfitidea点击:

How to easily consume a web service from PHP

phpweb-servicesvisual-studiowsdl

提问by Edo

Is there available any tool for PHP which can be used to generate code for consuming a web servicebased on its WSDL? Something comparable to clicking "Add Web Reference" in Visual Studio or the Eclipse plugin which does the same thing for Java.

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

采纳答案by pix0r

I've had great success with wsdl2php. It will automatically create wrapper classes for all objects and methods used in your web service.

我在wsdl2php 上取得了巨大的成功。它将自动为 Web 服务中使用的所有对象和方法创建包装类。

回答by DavidM

In PHP 5 you can use SoapClienton the WSDL to call the web service functions. For example:

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

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

and $client is now an object which has class methods as defined in some.wsdl. So if there was a method called getTime in the WSDL then you would just call:

$client 现在是一个对象,它具有在 some.wsdl 中定义的类方法。因此,如果 WSDL 中有一个名为 getTime 的方法,那么您只需调用:

$result = $client->getTime();

And the result of that would (obviously) be in the $result variable. You can use the __getFunctions method to return a list of all the available methods.

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

回答by Wally Lawless

I have used NuSOAPin the past. I liked it because it is just a set of PHP files that you can include. There is nothing to install on the web server and no config options to change. It has WSDL support as well which is a bonus.

我过去曾使用过NuSOAP。我喜欢它,因为它只是一组您可以包含的 PHP 文件。无需在 Web 服务器上安装任何内容,也无需更改配置选项。它也有 WSDL 支持,这是一个额外的好处。

回答by Hoan

This articleexplains how you can use PHP SoapClient to call a api web service.

文章介绍了如何使用PHP SoapClient的调用API Web服务。

回答by Mashudu Muleya

HI I got this from this site : http://forums.asp.net/t/887892.aspx?Consume+an+ASP+NET+Web+Service+with+PHP

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

The web service has method Addwhich takes two params:

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

<?php
    $client = new SoapClient("http://localhost/csharp/web_service.asmx?wsdl");

     print_r( $client->Add(array("a" => "5", "b" =>"2")));
?>

回答by Luke Wenke

Say you were provided the following:

假设您获得了以下信息:

<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>

and

<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>

Let's say that accessing http://thesite.com/said that the WSDL address is: http://thesite.com/PortalIntegratorService.svc?wsdl

假设访问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;
}

As you can see, the items specified in the XML are used in the PHP code though the LoginId value can be changed.

如您所见,虽然可以更改 LoginId 值,但在 PHP 代码中使用了 XML 中指定的项目。

回答by Vaibhav

Well, those features are specific to a tool that you are using for development in those languages.

好吧,这些功能特定于您在这些语言中用于开发的工具。

You wouldn't have those tools if (for example) you were using notepad to write code. So, maybe you should ask the question for the tool you are using.

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

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

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