从 .Net 调用 PHP Web 服务

发布于 2024-11-26 18:12:34 字数 2807 浏览 0 评论 0 原文

我已经用 PHP 创建了一个 Web 服务,并尝试从我的 C# 代码中调用它。

当我尝试使用 wsdl 实用程序创建代理时

wsdl http://localhost:5365/DemoService.php?wsdl

我收到此错误

错误:找不到 http://myserver.co.za/sayHello:sayHelloPortType.
缺少命名空间 http://myserver.co.za/sayHello 的服务说明。
参数名称:名称

这是我的Webservice 代码(DemoService.php)

<?php

    function sayHello($name){
        $salutation = "Hi $name !";
        return $salutation;
    }

    $server = new SoapServer("greetings.wsdl");
    $server->addFunction("sayHello");
    $server->handle();

?>

和我的WSDL 代码(greetings.wsdl)

<?xml version ='1.0' encoding ='UTF-8' ?> 

<definitions name='greetings' 
  targetNamespace='http://myserver.co.za/sayHello' 
  xmlns:tns=' http://myserver.co.za/sayHello' 
  xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/' 
  xmlns:xsd='http://www.w3.org/2001/XMLSchema' 
  xmlns:soapenc='http://schemas.xmlsoap.org/soap/encoding/' 
  xmlns:wsdl='http://schemas.xmlsoap.org/wsdl/' 
  xmlns='http://schemas.xmlsoap.org/wsdl/'> 

  <message name='sayHelloRequest'> 
    <part name='name' type='xsd:string'/> 
  </message> 

  <message name='sayHelloResponse'> 
    <part name='salutation' type='xsd:string'/> 
  </message> 

  <portType name='sayHelloPortType'> 
    <operation name='sayHello'> 
      <input message='tns:sayHelloRequest'/> 
      <output message='tns:sayHelloResponse'/> 
    </operation> 
  </portType> 

  <binding name='sayHelloBinding' type='tns:sayHelloPortType'> 
    <soap:binding style='rpc' 
      transport='http://schemas.xmlsoap.org/soap/http'/> 
    <operation name='sayHello'> 
      <soap:operation soapAction=''/> 
      <input> 
        <soap:body use='encoded' namespace='' 
          encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/> 
      </input> 
      <output> 
        <soap:body use='encoded' namespace='' 
          encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/> 
      </output> 
    </operation> 
  </binding> 

  <documentation>This is Wiley's SOAP server Example</documentation>

  <service name='sayHelloService'> 
    <port name='sayHelloPort' binding='sayHelloBinding'> 
      <soap:address location='http://localhost:5365/DemoService.php'/> 
    </port> 
  </service> 

</definitions>

我真的不明白它想说什么。有人能指出我正确的方向吗?

I have created a Webservice in PHP and I'm trying to call it from my C# code.

When I try to create a proxy using wsdl utility

wsdl http://localhost:5365/DemoService.php?wsdl

I get this errors

Error: Cannot find definition for http://myserver.co.za/sayHello:sayHelloPortType.
Service Description with namespace http://myserver.co.za/sayHello is missing.
Parameter name: name

Here's my Webservice code (DemoService.php)

<?php

    function sayHello($name){
        $salutation = "Hi $name !";
        return $salutation;
    }

    $server = new SoapServer("greetings.wsdl");
    $server->addFunction("sayHello");
    $server->handle();

?>

and my WSDL code (greetings.wsdl)

<?xml version ='1.0' encoding ='UTF-8' ?> 

<definitions name='greetings' 
  targetNamespace='http://myserver.co.za/sayHello' 
  xmlns:tns=' http://myserver.co.za/sayHello' 
  xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/' 
  xmlns:xsd='http://www.w3.org/2001/XMLSchema' 
  xmlns:soapenc='http://schemas.xmlsoap.org/soap/encoding/' 
  xmlns:wsdl='http://schemas.xmlsoap.org/wsdl/' 
  xmlns='http://schemas.xmlsoap.org/wsdl/'> 

  <message name='sayHelloRequest'> 
    <part name='name' type='xsd:string'/> 
  </message> 

  <message name='sayHelloResponse'> 
    <part name='salutation' type='xsd:string'/> 
  </message> 

  <portType name='sayHelloPortType'> 
    <operation name='sayHello'> 
      <input message='tns:sayHelloRequest'/> 
      <output message='tns:sayHelloResponse'/> 
    </operation> 
  </portType> 

  <binding name='sayHelloBinding' type='tns:sayHelloPortType'> 
    <soap:binding style='rpc' 
      transport='http://schemas.xmlsoap.org/soap/http'/> 
    <operation name='sayHello'> 
      <soap:operation soapAction=''/> 
      <input> 
        <soap:body use='encoded' namespace='' 
          encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/> 
      </input> 
      <output> 
        <soap:body use='encoded' namespace='' 
          encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/> 
      </output> 
    </operation> 
  </binding> 

  <documentation>This is Wiley's SOAP server Example</documentation>

  <service name='sayHelloService'> 
    <port name='sayHelloPort' binding='sayHelloBinding'> 
      <soap:address location='http://localhost:5365/DemoService.php'/> 
    </port> 
  </service> 

</definitions>

I really don't understand what it is trying to say. Can some one point me in a right direction?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

吲‖鸣 2024-12-03 18:12:34

这是 WSDL 的问题

所在 首先,xmlns:tns 命名空间的开头有一个空格

 xmlns:tns=' http://myserver.co.za/sayHello' <-- Bad
 xmlns:tns='http://myserver.co.za/sayHello'  <-- Good

接下来, 节点位于错误的位置,它应该位于 节点内,如下所示

<service ...>
    <documentation>This is Wiley's SOAP server Example</documentation>
    <port ...>
        ...
    </port>
</service>

您的端口绑定元素需要使用 tns 命名空间

<port name='sayHelloPort' binding='sayHelloBinding'>  <-- Bad
<port name='sayHelloPort' binding='tns:sayHelloBinding'>  <-- Good

最后我无法获取 soap:body 导入为encoded,并且必须将它们交换为literal,还要注意它们需要在 namespace 元素中提供一个值

<soap:body use='literal' namespace='urn:xmethods-delayed-quotes' encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/> 

我相信 节点中的 soapAction 元素仍然需要一个值才能正常工作,例如 urn:xmethods -delayed-quotes#sayHello,但没有它也会导入。

完整的 WSDL(我可以使用 WSDL.exe 导入它,不会出现错误)

<?xml version ='1.0' encoding ='UTF-8' ?> 
<definitions name='greetings' 
  targetNamespace='http://myserver.co.za/sayHello' 
  xmlns:tns='http://myserver.co.za/sayHello' 
  xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/' 
  xmlns:xsd='http://www.w3.org/2001/XMLSchema' 
  xmlns:soapenc='http://schemas.xmlsoap.org/soap/encoding/' 
  xmlns:wsdl='http://schemas.xmlsoap.org/wsdl/' 
  xmlns='http://schemas.xmlsoap.org/wsdl/'> 

  <message name='sayHelloRequest'> 
    <part name='name1' type='xsd:string'/> 
  </message> 

  <message name='sayHelloResponse'> 
    <part name='salutation' type='xsd:string'/> 
  </message> 

  <portType name='sayHelloPortType'> 
    <operation name='sayHello'> 
      <input message='tns:sayHelloRequest'/> 
      <output message='tns:sayHelloResponse'/> 
    </operation> 
  </portType> 

  <binding name='sayHelloBinding' type='tns:sayHelloPortType'> 
    <soap:binding style='rpc' transport='http://schemas.xmlsoap.org/soap/http'/> 
    <operation name='sayHello'> 
      <soap:operation soapAction=''/> 
      <input>  
        <soap:body use='literal' namespace='urn:xmethods-delayed-quotes' encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/> 
      </input> 
      <output> 
        <soap:body use='literal' namespace='urn:xmethods-delayed-quotes'  encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/> 
      </output> 
    </operation> 
  </binding> 
  <service name='sayHelloService'> 
    <documentation>Service Description</documentation>
    <port name='sayHelloPort' binding='tns:sayHelloBinding'> 
      <soap:address location='http://localhost:5365/DemoService.php'/> 
    </port> 
  </service> 
</definitions>

Here is what is wrong with the WSDL

First the xmlns:tns namespace has a space at the start of it

 xmlns:tns=' http://myserver.co.za/sayHello' <-- Bad
 xmlns:tns='http://myserver.co.za/sayHello'  <-- Good

Next the <documentation> node is in the wrong place, it should be inside the <service> node like so

<service ...>
    <documentation>This is Wiley's SOAP server Example</documentation>
    <port ...>
        ...
    </port>
</service>

Your port binding element needs to use the tns namespace

<port name='sayHelloPort' binding='sayHelloBinding'>  <-- Bad
<port name='sayHelloPort' binding='tns:sayHelloBinding'>  <-- Good

Finally I could not get the soap:body to import as encoded, and had to swap them to literal, also note they need a value in the namespace element

<soap:body use='literal' namespace='urn:xmethods-delayed-quotes' encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/> 

I believe the soapAction element in the <soap:operation soapAction=''/> node still needs a value to work correctly, something like urn:xmethods-delayed-quotes#sayHello, but it will import without it.

Full WSDL (I caan import this using WSDL.exe without error)

<?xml version ='1.0' encoding ='UTF-8' ?> 
<definitions name='greetings' 
  targetNamespace='http://myserver.co.za/sayHello' 
  xmlns:tns='http://myserver.co.za/sayHello' 
  xmlns:soap='http://schemas.xmlsoap.org/wsdl/soap/' 
  xmlns:xsd='http://www.w3.org/2001/XMLSchema' 
  xmlns:soapenc='http://schemas.xmlsoap.org/soap/encoding/' 
  xmlns:wsdl='http://schemas.xmlsoap.org/wsdl/' 
  xmlns='http://schemas.xmlsoap.org/wsdl/'> 

  <message name='sayHelloRequest'> 
    <part name='name1' type='xsd:string'/> 
  </message> 

  <message name='sayHelloResponse'> 
    <part name='salutation' type='xsd:string'/> 
  </message> 

  <portType name='sayHelloPortType'> 
    <operation name='sayHello'> 
      <input message='tns:sayHelloRequest'/> 
      <output message='tns:sayHelloResponse'/> 
    </operation> 
  </portType> 

  <binding name='sayHelloBinding' type='tns:sayHelloPortType'> 
    <soap:binding style='rpc' transport='http://schemas.xmlsoap.org/soap/http'/> 
    <operation name='sayHello'> 
      <soap:operation soapAction=''/> 
      <input>  
        <soap:body use='literal' namespace='urn:xmethods-delayed-quotes' encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/> 
      </input> 
      <output> 
        <soap:body use='literal' namespace='urn:xmethods-delayed-quotes'  encodingStyle='http://schemas.xmlsoap.org/soap/encoding/'/> 
      </output> 
    </operation> 
  </binding> 
  <service name='sayHelloService'> 
    <documentation>Service Description</documentation>
    <port name='sayHelloPort' binding='tns:sayHelloBinding'> 
      <soap:address location='http://localhost:5365/DemoService.php'/> 
    </port> 
  </service> 
</definitions>
会发光的星星闪亮亮i 2024-12-03 18:12:34

为什么不通过以下方式添加对Web服务的引用:

右键单击项目文件->添加网络参考 -->输入 Web 服务的 URL,瞧!

这应该在 Web.config(或 App.config)中创建必要的条目以及您将在应用程序中使用的代理类。

Why don't you add the reference to the web service through:

right-click on project file -> add web reference --> type in the url to the webservice and voila!

This should create the necessary entries in Web.config (or App.config) plus the proxy classes that you'll use in your app.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文