user3734231
user3734231

Reputation: 36

Sending XML using Curl post to a webservice

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

Upvotes: 0

Views: 5157

Answers (1)

J Charles
J Charles

Reputation: 390

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.

Upvotes: 1

Related Questions