【问题标题】:PHP SoapClient Connect to NET Web ServicePHP SoapClient 连接到 NET Web 服务
【发布时间】:2014-09-12 22:29:09
【问题描述】:

我在创建从 PHP 应用程序到 .NET Web 服务的连接时遇到了一些问题。我已经在很多网站上阅读了要做什么,但我似乎无法让它发挥作用。

Web 服务需要用户名和密码,而我要做的只是通过发送用户 ID 在用户数据管理系统中获取一些用户配置文件数据。

这就是提供者所说的 SOAP 请求应该是这样的:

POST /feed30/clientdataservice.asmx HTTP/1.1
Host: ws-ic-pilot.acme.com
Content-Type: text/xml; charset=utf-8
Content-Length: length
SOAPAction: "GetUser"

<?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>
<GetUser xmlns="urn:Acme:ClientDataService">
  <strUserId>string</strUserId>
</GetUser>
</soap:Body>
</soap:Envelope>

这是我根据我在互联网上各个网站上找到的内容编写的代码:

$client = new SoapClient('https://ws-ic-pilot.acme.com/feed30/clientdataservice.asmx
WSDL',Array("trace" => 1));
$header = new SoapHeader(
    array(
            'UserName' => 'myusername',
            'Password' => 'mypassword',
            'strUserID' => '12345'
    ) );

$client->__setSoapHeaders($header);

$client->GetUser();

当我使用 __getLastRequestHeaders 和 __getLastRequest 时,我正在创建的 SOAP 消息如下所示:

POST /feed30/clientdataservice.asmx HTTP/1.1
Host: ws-pilot.acme.com
Connection: Keep-Alive
User-Agent: PHP-SOAP/5.3.21
Content-Type: text/xml; charset=utf-8
SOAPAction: "GetUser"
Content-Length: 247

<?xml version="1.0" encoding="UTF-8"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ns1="urn:Acme:ClientDataService"><SOAP-ENV:Header/>
<SOAP-ENV:Body><ns1:GetUser/>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>

当我将 SOAP 消息发布到 .NET 服务器时,我得到以下响应:

<ExceptionInfo>Exception Message:       Security requirements are not satisfied because the  
security header is not present in the incoming message.;Exception Target Method:
 ValidateMessageSecurity</ExceptionInfo></detail>
</soap:Body></soap:Envelope>

任何人都可以就我如何解决此问题提供建议/建议吗?

提前感谢您的帮助!

【问题讨论】:

    标签: php web-services soap


    【解决方案1】:

    所以我找到了我的问题的答案。就我而言,似乎 SOAPClient 不是最好的方法。相反,更好的方法是通过 CURL 创建、发送和接收 SOAP 消息。下面是我在另一个 Stackoverflow 页面上找到的示例。我根据自己的情况对其进行了调整,效果很好!

    $credentials = "username:pass"; 
    $url = "https://url/folder/sample.wsdl";
    $body = ''; /// Your SOAP XML needs to be in this variable
    
    $headers = array( 
    'Content-Type: text/xml; charset="utf-8"', 
    'Content-Length: '.strlen($body), 
    'Accept: text/xml', 
    'Cache-Control: no-cache', 
    'Pragma: no-cache', 
    'SOAPAction: "customerSearch"'
    ); 
    
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_TIMEOUT, 60); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($ch, CURLOPT_USERAGENT, $defined_vars['HTTP_USER_AGENT']);
    
    curl_setopt($ch, CURLOPT_POST, true); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $body); 
    curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($ch, CURLOPT_USERPWD, $credentials);
    
    $data = curl_exec($ch); 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      • 2014-10-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多