I'm trying to send XML to a remote site. I originally coded it using curl. That failed (no response) and the rep for the company said it was because I was using curl. So I changed the code to not use it but it fails with
file_get_contents(https://testws.atdconnect.com/ws/3_4/locations.wsdl/) [function.file-get-contents]: failed to open stream: HTTP request failed! HTTP/1.1 500 Internal Server Error
The code I am using is shown below. The first call tries to send with curl while the second doesn't. Can someone point out where I am going wrong, please?
$client_id = "the id";
$username = "the user";
$password = "the pass";
$url = "https://testws.atdconnect.com/ws/3_4/locations.wsdl/";
echo GetDistributionCenter($url, $client_id, $username, $password);
echo GetDistributionCenter($url, $client_id, $username, $password, true);
function GetDistributionCenter($url, $client_id, $username, $password, $send_array = false) {
$xml_str = '
<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:loc="http://api.atdconnect.com/atd/3_4/locations">
<soapenv:Header>
<wsse:Security soapenv:mustUnderstand="1" xmlns:wsse="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">
<wsse:UsernameToken atd:clientId="' . $client_id . '" xmlns:atd="http://api.atdconnect.com/atd">
<wsse:Username>' . $username . '</wsse:Username>
<wsse:Password Type="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-username-token-profile-1.0#PasswordText">' . $password . '</wsse:Password>
</wsse:UsernameToken>
</wsse:Security>
</soapenv:Header>
<soapenv:Body>
<loc:getDistributionCenterRequest>
<loc:servicingDC>070</loc:servicingDC>
</loc:getDistributionCenterRequest>
</soapenv:Body>
</soapenv:Envelope>';
if ($send_array) {
$xml_str = array($xml_str);
$xml_str = http_build_query($xml_str);
return SendPostRequest($url, $xml_str);
}
return SendXMLCommand($url, $xml_str);
}
function SendPostRequest($url, $postdata) {
$opts = array('http' =>
array(
'method' => 'POST',
'header' => 'Content-type: application/x-www-form-urlencoded',
'Content-Length: ' . strlen($postdata) . "
",
'content' => $postdata
)
);
$context = stream_context_create($opts);
return file_get_contents($url, false, $context);
}
function SendXMLCommand($url, $parsed_data) {
$ch = curl_init();
$timeout = 5;
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt ($ch, CURLOPT_POSTFIELDS, $parsed_data);
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, true);
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt ($ch, CURLOPT_HTTPHEADER, array("Content-Type: text/xml","SOAPAction: \"/soap/action/query\"", "Content-length: ".strlen($data)));
$result = curl_exec($ch);
if (curl_error($ch)) {
$result = 'Curl error: ' . curl_error($ch);
}
curl_close($ch);
return $result;
}