【问题标题】:How to easily consume a web service from PHP如何从 PHP 轻松使用 Web 服务
【发布时间】:2010-09-05 11:43:21
【问题描述】:

是否有任何 PHP 工具可用于根据 WSDL 生成使用 web service 的代码?类似于在 Visual Studio 中单击“添加 Web 引用”或对 Java 执行相同操作的 Eclipse 插件。

【问题讨论】:

    标签: php web-services visual-studio wsdl


    【解决方案1】:

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

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

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

    $result = $client->getTime();
    

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

    【讨论】:

    【解决方案2】:

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

    【讨论】:

    • 炸弹试图解析 VMWares vimService wsdl
    • 更新:链接已更改并且现在可以工作,指向 github,因此路过的用户不应因为旧的(当时非常有用)评论而跳过工作链接:)
    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

      article 解释了如何使用 PHP SoapClient 调用 api Web 服务。

      【讨论】:

        【解决方案5】:

        嗯,这些功能特定于您用于以这些语言进行开发的工具。

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

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

        【讨论】:

          【解决方案6】:

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

          网络服务有方法Add,它接受两个参数:

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

          【讨论】:

            【解决方案7】:

            假设您获得了以下内容:

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

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

            【讨论】:

              猜你喜欢
              • 2013-05-29
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2020-02-14
              • 1970-01-01
              • 2012-01-27
              • 1970-01-01
              相关资源
              最近更新 更多