使用Curl post将XML发送到Web服务

时间:2014-06-17 09:23:14

标签: php web-services curl soap wsdl

我正在尝试将XML脚本发送到Web服务器以检索身份验证令牌,我希望得到一些帮助。目前我的代码我认为它正在连接,但它只返回屏幕上文本格式的wsdl文件。

我想收到验证令牌。

我的代码:

<?php

$xml_data = '
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
 xmlns:ns="http://dpd.com/common/service/types/LoginService/2.0">
 <soapenv:Header/>
 <soapenv:Body>
 <ns:getAuth>
 <delisId>id</delisId>
 <password>password</password>
 <messageLanguage>nl_NL</messageLanguage>
 </ns:getAuth>
 </soapenv:Body>
<soapenv:Envelope>
';

$headers = array(
"POST  HTTP/1.1",
"Host: hostname",
"Content-type: application/soap+xml; charset=\"utf-8\"",
"SOAPAction: \"http://dpd.com/common/service/LoginService/2.0/getAuth\"",
"Content-length: ".strlen($xml_data)
);

$url = 'https://public-ws-stage.dpd.com/services/LoginService/V2_0/?wsdl';

$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, "$xml_data");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$output = curl_exec($ch);
$err = curl_error($ch);
print_r($output);
print_r($err);

curl_close($ch);

?>

WSDL文件位于以下链接中: https://public-ws-stage.dpd.com/services/LoginService/V2_0/?wsdl

1 个答案:

答案 0 :(得分:1)

在这里,你可以享受一下吧:

$xml_data = '
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
 xmlns:ns="http://dpd.com/common/service/types/LoginService/2.0">
 <soapenv:Header/>
 <soapenv:Body>
 <ns:getAuth>
 <delisId>id</delisId>
 <password>password</password>
 <messageLanguage>nl_NL</messageLanguage>
 </ns:getAuth>
 </soapenv:Body>
<soapenv:Envelope>
';

$headers = array(
"POST  HTTP/1.1",
"Host: hostname",
"Content-type: application/soap+xml; charset=\"utf-8\"",
"SOAPAction: \"http://dpd.com/common/service/LoginService/2.0/getAuth\"",
"Content-length: ".strlen($xml_data)
);

$url = 'https://public-ws-stage.dpd.com/services/LoginService/V2_0/?wsdl';

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE); //Don't verify ssl certificate
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data);

$reply = curl_exec($ch); 

// Represents an element in an XML document.
$xmli = new SimpleXMLElement($reply);

// prints the XML response
print_r($reply);
// prints the XML object
print_r($xmli);

我已将SimpleXMLElement类包含在您想要作为对象访问响应数据的位置。