dongzhao5970 2018-11-02 15:49
浏览 84

发送XML失败

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;
    }
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 Python爬取指定微博话题下的内容,保存为txt
    • ¥15 vue2登录调用后端接口如何实现
    • ¥65 永磁型步进电机PID算法
    • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
    • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
    • ¥15 如何处理复杂数据表格的除法运算
    • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)
    • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
    • ¥15 latex怎么处理论文引理引用参考文献
    • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?