代码之家  ›  专栏  ›  技术社区  ›  cfeduke

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

  •  3
  • cfeduke  · 技术社区  · 16 年前

    soapval false string array s假设我的web服务的WSDL由 http://localhost:3333/Service.asmx?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 WebMethod (没有重命名,它的字面意思是 DoSomething )看到这些参数都是默认值( int 0 这个 null

    Param 打字正确吗?

    3 回复  |  直到 16 年前
        1
  •  6
  •   Community rcollyer    12 年前

    <?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);
    ?>
    
        2
  •  3
  •   jishi    16 年前

    有点不相关,但自从PHP5以来,您就有了对SOAP的本机支持。

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

    那可能更方便一点。

    http://se.php.net/soap

        3
  •  1
  •   Vladislav    14 年前

    下面是本机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" />