无法从 PHP 调用 .NET ASMX

Unable to call .NET ASMX from PHP

我有以下 - 当前托管 - 在 .NET 中创建的 SOAP 服务,我正试图从 PHP 调用:

POST /ExampleService/ExampleService.asmx HTTP/1.1
Host: dev.examplesite.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "http://localhost:51713/ExampleService.asmx/RegisterPerson"

<?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:Header>
    <UserCredentials xmlns="http://localhost:51713/ExampleService.asmx">
      <UserID>string</UserID>
      <AuthKey>string</AuthKey>
    </UserCredentials>
  </soap:Header>
  <soap:Body>
    <RegisterPerson xmlns="http://localhost:51713/ExampleService.asmx">
      <requestItem>
        <RequestResult>string</RequestResult>
        <Firstname>string</Firstname>
        <Lastname>string</Lastname>
      </requestItem>
    </RegisterPerson>
  </soap:Body>
</soap:Envelope>

我正在尝试使用以下 PHP 代码调用它:

<?php
    $ns = "http://dev.examplesite.com/ExampleService/ExampleService.asmx";  
    $wsdl_url = "http://dev.examplesite.com/ExampleService/ExampleService.asmx?wsdl";
    $client = new SOAPClient($wsdl_url);

    $header = new SoapHeader(
        $ns, 
        'UserCredentials', 
        array(
            'UserID' => "1", 
            'AuthKey' => "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX" 
        )
    );
    $client->__setSoapHeaders($header);

    $params = array( 
        'Firstname'  => 'John', 
        'Lastname' => 'Doe'
    );

    $client->__soapCall('RegisterPerson',$params);
?>

然而,这会导致以下错误:

Fatal error: Uncaught SoapFault exception: [soap:MustUnderstand] Missing required header 'UserCredentials'. in /home/devops1/public_html/asmxtest/register.php:43 Stack trace: #0 /home/devops1/public_html/asmxtest/register.php(43): SoapClient->__soapCall('RegisterPerso...', Array) #1 {main} thrown in /home/devops1/public_html/asmxtest/register.php on line 43

我尝试了其他几种方法,但都没有成功。让我担心的一件事是,我们正在 通过电线 到达这个已经托管在测试服务器上的 Web 服务,并且 localhost:51713敲响了警钟。是否应将其更改为完全限定的域名,例如 dev.examplesite.com?

尽管 SoapClient 应该如此简单,但我从未成功过。在几乎每种情况下,我们都推出了自己的流程,似乎总是效果更好。

示例:

// set our headers to pass in cURL
$headers = array(
    'Content-type: text/xml;charset="utf-8"',
    'Accept: text/xml',
    'Cache-Control: no-cache',
    'Pragma: no-cache',
    'SOAPAction: ' . $action,
    'Content-length: '.strlen($soap)
 );

 // initiate cURL
 try {
    $_ch = curl_init();
    curl_setopt($_ch, CURLOPT_SSL_VERIFYPEER, 0);
    curl_setopt($_ch, CURLOPT_URL, WEB_SERVICE_URL);
    curl_setopt($_ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($_ch, USERPWD, USER_ID . ":" . PASSWORD);
    curl_setopt($_ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
    curl_setopt($_ch, CURLOPT_TIMEOUT, 10);
    curl_setopt($_ch, CURLOPT_POST, true);
    curl_setopt($_ch, CURLOPT_POSTFIELDS, $soap);
    curl_setopt($_ch, CURLOPT_HTTPHEADER, $headers);

    // process the request and get the result back
    $response = curl_exec($_ch);
    curl_close($_ch);
    return $response;
 } catch (Exception $e) {
    capDebug(__FILE__, __LINE__, "Error calling the web service: " . $e->getMessage(), "/tmp/errors.log");
    return false;
 }

之后我们不得不修改数据,但它总是有效!