如何通过 PHP 调用 C# Web 服务?

发布于 2024-07-08 20:33:34 字数 2070 浏览 11 评论 0原文

我已经使用 ASP.NET(用 C#)编写了一个 Web 服务,并且正在尝试使用 NuSOAP 编写一个示例 PHP 客户端。 我遇到困难的地方是如何做到这一点的例子; 有些显示正在使用soapval(我不太了解参数 - 例如将false作为string类型传递等),而其他人只是使用直接的数组。 假设 http://localhost:3333/Service.asmx?wsdl 报告的我的 Web 服务的 WSDL 看起来像:

POST /Service.asmx HTTP/1.1
Host: localhost
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/webservices/DoSomething"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <DoSomething xmlns="http://tempuri.org/webservices">
      <anId>int</anId>
      <action>string</action>
      <parameters>
        <Param>
          <Value>string</Value>
          <Name>string</Name>
        </Param>
        <Param>
          <Value>string</Value>
          <Name>string</Name>
        </Param>
      </parameters>
    </DoSomething>
  </soap:Body>
</soap:Envelope>

我的第一次 PHP 尝试看起来像:

<?php
require_once('lib/nusoap.php');
$client = new nusoap_client('http://localhost:3333/Service.asmx?wsdl');

$params = array(
    'anId' => 3, //new soapval('anId', 'int', 3),
    'action' => 'OMNOMNOMNOM',
    'parameters' => array(
        'firstName' => 'Scott',
        'lastName' => 'Smith'
    )
);
$result = $client->call('DoSomething', $params, 'http://tempuri.org/webservices/DoSomething', 'http://tempuri.org/webservices/DoSomething');
print_r($result);
?>

现在除了 Param 类型是我很确定我的简单 $array 尝试不会自动使用复杂类型,我在 Web 服务中设置断点并看到我标记为 WebMethod (没有重命名它,它的字面意思是 DoSomething),并且看到参数都是默认值(int0stringnull 等)。

我的 PHP 语法应该是什么样子,我需要做什么才能正确传递 Param 类型?

I've written a web service using ASP.NET (in C#) and I'm attempting to write an example PHP client using NuSOAP. Where I'm tripped up on are examples of how to do this; some show soapval being used (and I don't quite understand the parameters - for example passing false as string types, etc.), while others are just using straight arrays. Let's say the WSDL for my web service as reported by http://localhost:3333/Service.asmx?wsdl looks something like:

POST /Service.asmx HTTP/1.1
Host: localhost
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://tempuri.org/webservices/DoSomething"

<?xml version="1.0" encoding="utf-8"?>
<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
  <soap:Body>
    <DoSomething xmlns="http://tempuri.org/webservices">
      <anId>int</anId>
      <action>string</action>
      <parameters>
        <Param>
          <Value>string</Value>
          <Name>string</Name>
        </Param>
        <Param>
          <Value>string</Value>
          <Name>string</Name>
        </Param>
      </parameters>
    </DoSomething>
  </soap:Body>
</soap:Envelope>

My first PHP attempt looks like:

<?php
require_once('lib/nusoap.php');
$client = new nusoap_client('http://localhost:3333/Service.asmx?wsdl');

$params = array(
    'anId' => 3, //new soapval('anId', 'int', 3),
    'action' => 'OMNOMNOMNOM',
    'parameters' => array(
        'firstName' => 'Scott',
        'lastName' => 'Smith'
    )
);
$result = $client->call('DoSomething', $params, 'http://tempuri.org/webservices/DoSomething', 'http://tempuri.org/webservices/DoSomething');
print_r($result);
?>

Now aside from the Param type being a complex type which I'm pretty sure my simple $array attempt will not automagically work with, I'm breakpointing in my web service and seeing the method I've marked as WebMethod (without renaming it, its literally DoSomething) and seeing the arguments are all default values (the int is 0, the string is null, etc.).

What should my PHP syntax look like, and what do I have to do to pass the Param type correctly?

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

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

发布评论

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

评论(3

罗罗贝儿 2024-07-15 20:33:34

你必须将东西包装在大量的嵌套数组中。

<?php
require_once('lib/nusoap.php');
$client = new nusoap_client('http://localhost:3333/Service.asmx?wsdl');

$params = array(
      'anId' => 3,
      'action' => 'OMNOMNOMNOM',
      'parameters' => array(
              'Param' => array(
                  array('Name' => 'firstName', 'Value' => 'Scott'),
                  array('Name' => 'lastName', 'Value' => 'Smith')
                       )
      )
);
$result = $client->call('DoSomething', array($params), 
                'http://tempuri.org/webservices/DoSomething', 
                'http://tempuri.org/webservices/DoSomething');
print_r($result);
?>

You have to wrap things in tons of nested arrays.

<?php
require_once('lib/nusoap.php');
$client = new nusoap_client('http://localhost:3333/Service.asmx?wsdl');

$params = array(
      'anId' => 3,
      'action' => 'OMNOMNOMNOM',
      'parameters' => array(
              'Param' => array(
                  array('Name' => 'firstName', 'Value' => 'Scott'),
                  array('Name' => 'lastName', 'Value' => 'Smith')
                       )
      )
);
$result = $client->call('DoSomething', array($params), 
                'http://tempuri.org/webservices/DoSomething', 
                'http://tempuri.org/webservices/DoSomething');
print_r($result);
?>
风为裳 2024-07-15 20:33:34

有点不相关,但从 PHP5 开始,您就拥有了对 SOAP 的本机支持。

$client = new SoapClient("some.wsdl");
$client->DoSomething($params);

这样可能会方便一些。

http://se.php.net/soap

Sort of unrelated but since PHP5 you have native support for SOAP.

$client = new SoapClient("some.wsdl");
$client->DoSomething($params);

That might be a little more convenient.

http://se.php.net/soap

吃颗糖壮壮胆 2024-07-15 20:33:34

这里是具有本机 SOAP 支持的示例:

    // Create a new soap client based on the service's metadata (WSDL)
    $client = new SoapClient("http://some.wsdl",
        array('location' => 'http://127.0.0.100:80/IntegrationService/php'));

    $params = array();
    $params['lead']['Firstname']    = $user->firstname;
    $params['lead']['Lastname']     = $user->lastname;
    $params['lead']['Product']      = $product;
    $params['lead']['JobTitle']     = $user->job_title;
    $params['lead']['Email']        = $user->mail;
    $params['lead']['Phone']        = $user->phone;
    $params['lead']['CompanyName']  = $user->company_name;
    $params['lead']['City']         = $user->city;
    $params['lead']['Industry']     = $user->industry;

    $client->SubmitLead($params);

其中 SoapClient 描述中的“.../IntegrationService/php”是 WCF 中的端点:

<endpoint
            address="php"
            binding="basicHttpBinding"
            contract="Integration.Service.IDrupalIntegrationService" />

Here the sample with native SOAP support:

    // Create a new soap client based on the service's metadata (WSDL)
    $client = new SoapClient("http://some.wsdl",
        array('location' => 'http://127.0.0.100:80/IntegrationService/php'));

    $params = array();
    $params['lead']['Firstname']    = $user->firstname;
    $params['lead']['Lastname']     = $user->lastname;
    $params['lead']['Product']      = $product;
    $params['lead']['JobTitle']     = $user->job_title;
    $params['lead']['Email']        = $user->mail;
    $params['lead']['Phone']        = $user->phone;
    $params['lead']['CompanyName']  = $user->company_name;
    $params['lead']['City']         = $user->city;
    $params['lead']['Industry']     = $user->industry;

    $client->SubmitLead($params);

Where '.../IntegrationService/php' in SoapClient description is endpoint in WCF:

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