django或php curl发布xml数据

时间:2012-02-28 10:39:02

标签: php xml django curl

我想通过发送此xml来执行此付款获取请求。

<?xml version="1.0" encoding="UTF-8"?>
<COBROAIRLINES>
<paymentService>
<id_company>0000</id_company>
<id_branch>5</id_branch>
<country>MEX</country>
<user>USUARIO</user>
<password>DE3C9FEF064F1459C30C0E7B</password>
<merchant>00000</merchant>
<reference>123pba</reference>
<tp_operation>12</tp_operation>
<creditCardType>V/MC</creditCardType>
<creditCardName>NOMBRE TH</creditCardName>
<creditCardNumber>AF3992EE603B743FC70D0D71F858C63C</creditCardNumber>
<creditCardExpMonth>AB3D</creditCardExpMonth>
<creditCardExpYear>AA3E</creditCardExpYear>
<creditCardCVV>291</creditCardCVV>
<initialDeferment>00</initialDeferment>
<numberOfPayments>06</numberOfPayments>
<planType>03</planType>
</paymentService>
<transaction>
<transactionTS>2008/01/01 10:00:00</transactionTS>
<currency>MXN</currency>
<orderAmount>1000</orderAmount>
<orderTotalTickets>1</orderTotalTickets>
<orderPromo>0</orderPromo>
<orderSource>000</orderSource>
<sdos>1</sdos>
</transaction>
<passengerFlight>
<passengerName>Juan Perez</passengerName>
<passengerTicketAmount>1000</passengerTicketAmount>
<passengerFrequentFlierNumber>96321</passengerFrequentFlierNumber>
<flightFare>A</flightFare>
<flightRoundTrip>0</flightRoundTrip>
<flightOriginCity>MEX</flightOriginCity>
<flightOriginCountry>MEX</flightOriginCountry>
<flightDestinationCity>TKY</flightDestinationCity>
<flightDestinationCountry>JAP</flightDestinationCountry>
<flightDaysTillDeparture>1</flightDaysTillDeparture>
<flightLayovers>0</flightLayovers>
</passengerFlight>
<userSalutation>Ing.</userSalutation>
<userName>Juan Perez</userName>
<userStreet>Heriberto Frias 1527</userStreet>
<userNeighborhood>Del Valle</userNeighborhood>
Confidential 13 de 25 Airlines Payment Iv6.0
<userCity>Mexico</userCity>
<userState>DF</userState>
<userZipCode>03100</userZipCode>
<userCountry>Mexico</userCountry>
<userPhone>5554425700</userPhone>
<userPhone2>5512345678</userPhone2>
<userEMail>correo@correo.com.mx</userEMail>
<userLogin>prueba</userLogin>
<userReturnCustomer>0</userReturnCustomer>
<billingName>Juan Perez</billingName>
<billingStreet>Corregidora</billingStreet>
<billingHouseNumber>92</billingHouseNumber>
<billingHouseNumberInt>1</billingHouseNumberInt>
<billingNeighborhood>Miguel Hidalgo</billingNeighborhood>
<billingMunicipality>Tlalpan</billingMunicipality>
<billingCity>Mexico</billingCity>
<billingState>DF</billingState>
<billingZipCode>14260</billingZipCode>
<billingCountry>Mexico</billingCountry>
<billingPhone>5515009000</billingPhone>
<billingPhone2>5515009020</billingPhone2>
<billingEMail>correo@correo.com</billingEMail>
<browserSessionID>org.apache.catalina.session.Sta...</browserSessionID>
<browserHostName>172.0.0.1</browserHostName>
<browserAccept>image/gif</browserAccept>
<browserAcceptEncoding>gzip, deflate</browserAcceptEncoding>
<browserAcceptCharset>ISO-8859-1,utf-8;q=0.7,*;q=0.7 </browserAcceptCharset>
<browserID>Mozilla/4.0 (compatible; MSIE 5.5)</browserID>
<browserIDLanguageCode>en-us,en;q=0.5</browserIDLanguageCode>
<browserCookie>CL=null</browserCookie>
<browserIP>172.452.16.23</browserIP>
<browserReferer>http://www.cualquiera.com</browserReferer>
<browserConnection>keep-alive</browserConnection>
<javascriptData> TF1;014;5;7;18068;6%2C0%2C6001%2C18000;....</javascriptData>
<customField1>ABCDEF</customField1>
<customField2></customField2>
<customField3></customField3>
<customField4></customField4>
<customField5></customField5>
</COBROAIRLINES>

付款途径的网址为https://dev.mitec.com.mx/wscobroSdos/CobroAirlines/?xml= 在付款获取的文档中,他们给出了xml = xml文件内容。 所以基本上我不知道它是GET或POST,但在xml中没有charachter我假设它将是POST。

所以我试过了,

$xmldatafile="payment.xml"; 
   $xmlData = file_get_contents($xmldatafile); 
  $URL = "https://dev.mitec.com.mx/wscobroSdos/CobroAirlines?xml=";

  $ch = curl_init();
  // curl_setopt($ch, CURLOPT_MUTE, 1);
  curl_setopt($ch, CURLOPT_URL, $URL); 
  curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
  curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
  curl_setopt($ch, CURLOPT_POST, 1);
  curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: text/xml'));
  curl_setopt($ch, CURLOPT_FOLLOWLOCATION ,1);
  curl_setopt($ch, CURLOPT_POSTFIELDS, $xmlData);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

  echo  $output = curl_exec($ch);
   if(curl_errno($ch)) {
    print curl_error($ch);  
}
  curl_close($ch);

它给了我这个回应不知道为什么我该怎么办? 我认为这是paygetway网站http://www.centrodepagos.com.mx/cdpweb/descargas.htm

得到的回应是:

     <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<CENTEROFPAYMENTS>
    <reference></reference>
    <response>error</response>
    <foliocpagos></foliocpagos>
    <auth></auth>
    <cd_response></cd_response>
    <cd_error>01</cd_error>
    <nb_error>El xml enviado no corresponde a ningun modelo</nb_error>
    <time>05:02:44</time>
    <date>28/02/2012</date>
    <voucher></voucher>
</CENTEROFPAYMENTS>

同样的api也尝试过django python,这是我的最终目标。

import pycurl
ncServerURL='https://dev.mitec.com.mx/wscobroSdos/CobroAirlines?xml='
binaryptr = open('payment.xml','rb').read()
c = pycurl.Curl()
c.setopt(pycurl.URL, ncServerURL)
c.setopt(pycurl.POST, 1)
c.setopt(pycurl.HTTPHEADER, ["Content-type: text/xml"])
# c.setopt(c.URL, ncServerURL + '?xml=' + binaryptr)
# c.setopt(pycurl.TIMEOUT, ncServerMaxTime)
# c.setopt(pycurl.CONNECTTIMEOUT, ncServerMaxTime)
c.setopt(pycurl.NOSIGNAL, 1) # disable signals, curl will be using other means besides signals to timeout.
c.setopt(pycurl.POSTFIELDS, binaryptr)
import StringIO
b = StringIO.StringIO()
c.setopt(pycurl.WRITEFUNCTION, b.write)
c.perform()
ncServerData = b.getvalue()
print ncServerData

得到的回应是:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<CENTEROFPAYMENTS>
    <reference></reference>
    <response>error</response>
    <foliocpagos></foliocpagos>
    <auth></auth>
    <cd_response></cd_response>
    <cd_error>01</cd_error>
    <nb_error>El xml enviado no corresponde a ningun modelo</nb_error>
    <time>05:02:44</time>
    <date>28/02/2012</date>
    <voucher></voucher>
</CENTEROFPAYMENTS>

尝试使用urllib2

import urllib2
import urllib
binaryptr = open('payment.xml','rb').read()
data = binaryptr
headers = {
  'Content-Type': 'application/soap+xml; charset=utf-8'
}
req = urllib2.Request('https://dev.mitec.com.mx/wscobroSdos/CobroAirlines?xml=', data, headers)
response = urllib2.urlopen(req)
the_page = response.read()
print the_page

得到与上述相同的错误。

1 个答案:

答案 0 :(得分:1)

看起来它想要url中的xml而不是body。这是非常糟糕的设计,但请尝试将其附加到?xml =

$URL = "https://dev.mitec.com.mx/wscobroSdos/CobroAirlines/?xml=" . $xmlData;