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

I am trying to send a XML script to a webserver to retrieve an authentication token, i would like some help with that. At the moment with my code i think it is connecting but it returns only the wsdl file in text format on the screen.

I would like to receive the autentication token.

My code:

<?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);

?>

The WSDL file is in the link below: https://public-ws-stage.dpd.com/services/LoginService/V2_0/?wsdl

douxianwu2221
douxianwu2221 我想通过cURL完成它是有原因的。但它现在正在努力。
6 年多之前 回复
dongwenghe2416
dongwenghe2416 你为什么不只使用SoapClient类?或者您希望由于某种原因由cURL专门完成它?
6 年多之前 回复

1个回答

Here you go, works a treat:

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

I've included the SimpleXMLElement class incase you wanted to access the response data as an object.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐