dongsuo0517 2014-09-12 22:29
浏览 57

PHP SoapClient连接到.NET Web服务

I'm having some trouble creating a connection from a PHP app to a .NET web service. I've read on quite a few websites what to do but I cannot seem to make it work.

The web service requires a username and password and all I am trying to do is to get some user profile data in a user data management system by sending the user's ID.

This is what the provider says the SOAP request should look like:

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>

Here's the code I have written based on what I found on various sites on the internet:

$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();

When I used __getLastRequestHeaders and __getLastRequest the SOAP message that I am creating looks like this:

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>

When I post the SOAP message to the .NET server I get the following response:

<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>

Can anyone provide a suggestion/recommendation as to how I fix this issue?

Thanks in advance for your assistance!

  • 写回答

1条回答 默认 最新

  • drju37335 2014-09-15 18:14
    关注

    So I found the answer to my question. Seems that SOAPClient was not the best way to do this in my case. Instead a better way to do this is to create, send and receive a SOAP message by CURL. Below is an example of what I found on another Stackoverflow page. I adapted it to my situation and it worked great!

    $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); 
    
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!