dsfsdf5646 2014-06-17 09:23
浏览 452

使用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

  • 写回答

1条回答 默认 最新

  • dtd14883 2014-06-17 10:03
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 虚拟机打包apk出现错误
  • ¥30 最小化遗憾贪心算法上界
  • ¥15 用visual studi code完成html页面
  • ¥15 聚类分析或者python进行数据分析
  • ¥15 逻辑谓词和消解原理的运用
  • ¥15 三菱伺服电机按启动按钮有使能但不动作
  • ¥15 js,页面2返回页面1时定位进入的设备
  • ¥50 导入文件到网吧的电脑并且在重启之后不会被恢复
  • ¥15 (希望可以解决问题)ma和mb文件无法正常打开,打开后是空白,但是有正常内存占用,但可以在打开Maya应用程序后打开场景ma和mb格式。
  • ¥20 ML307A在使用AT命令连接EMQX平台的MQTT时被拒绝