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 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题