发送XML失败

时间:2018-11-02 15:49:42

标签: php xml

我正在尝试将XML发送到远程站点。我最初使用curl编码。失败了(没有回应),该公司的代表说那是因为我正在使用curl。因此,我将代码更改为不使用它,但由于

而失败
  

file_get_contents(https://testws.atdconnect.com/ws/3_4/locations.wsdl/)[function.file-get-contents]:无法打开流:HTTP请求失败! HTTP / 1.1 500内部服务器错误

我正在使用的代码如下所示。第一个呼叫尝试使用curl发送,而第二个则不发送。有人可以指出我要去哪里了吗?

    $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) . "\r\n",
               '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;
    }

0 个答案:

没有答案