2015-08-03 2 views
1

Я не очень хорошо знаком с php и curl, вам нужно преобразовать предварительный запрос PHP cURL POST в эквивалент python.Преобразование запроса PHP Curl API PAYGATE на python

Это код из сайта шлюза оплаты, называемый paygate, и я использую их пример php API от developer.paygate.co.za/. Код, который я пытался преобразовать в Python ниже:

<?php 
//The PayGate PayXML URL 
define("SERVER_URL", "https://www.paygate.co.za/payxml/process.trans"); 


//Construct the XML document header 
$XMLHeader = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><!DOCTYPE protocol SYSTEM \"https://www.paygate.co.za/payxml/payxml_v4.dtd\">"; 

// - Then construct the full transaction XML 
$XMLTrans = '<protocol ver="4.0" pgid="10011013800" pwd="test"><authtx cref="ABCqwerty1234" cname="Patel Sunny" cc="5200000000000015" exp="032022" budp="0" amt="10000" cur="ZAR" cvv="123" rurl="http://localhost/pg_payxml_php_final.php" nurl="http://localhost/pg_payxml_php_notify.php" /></protocol>' 

// Construct the request XML by combining the XML header and transaction 
$Request = $XMLHeader.$XMLTrans; 


// Create the POST data header containing the transaction 
$header[] = "Content-type: text/xml"; 
$header[] = "Content-length: ".strlen($Request)."\r\n"; 
$header[] = $Request; 


// Use cURL to post the transaction to PayGate 
// - first instantiate cURL; if it fails then quit now. 
$ch = curl_init(); 
if (!$ch) die("ERROR: cURL initialization failed. Check your cURL/PHP configuration."); 

// - then set the cURL options; to ignore SSL invalid certificates; set timeouts etc. 
curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, 0); 
curl_setopt ($ch, CURLOPT_SSL_VERIFYHOST, 0); 
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt ($ch, CURLOPT_TIMEOUT, 60); 
curl_setopt ($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)"); 
curl_setopt ($ch, CURLOPT_CUSTOMREQUEST, "POST");    

// - then set the PayXML URL and the transaction data 
curl_setopt ($ch, CURLOPT_URL, SERVER_URL); 
curl_setopt ($ch, CURLOPT_HTTPHEADER, $header);    

// Connect to PayGate PayXML and send data 
$Response = curl_exec ($ch); 

// Checl for any connection errors and then close the connection. 
$curlError = curl_errno($ch); 
curl_close($ch); 

Я знаю об основных запросах в питоне, но не могу передать атрибуты в этой просьбе, я тоже запутался о передаче Curl данных в запросах.

Я пытаюсь это нравится:

import requests 
post_data = {'pgid':'10011013800', 
      'pwd':'test', 
      'cref': 'ABCX1yty36858gh', 
      'cname':'PatelSunny', 
      'cc':'5200000470000015', 
      'exp':'032022', 
      'budp':'0', 
      'amt':'50000', 
      'cur':'ZAR', 
      'cvv':'123', 
      'rurl':'http://localhost/pg_payxml_php_final.php', 
      'nurl':'http://localhost/pg_payxml_php_notify.php', 
      'submit':'Submit' 
      } 

r = requests.get('https://www.paygate.co.za/payxml/process.trans', params=post_data,headers=headers) 

# print(r.url) 
print r.text 

Но это показывает ошибку

405 - HTTP глагол используется для доступа к этой странице не допускается.

ответ

0

Наконец решил ее,

import requests 
import xml.etree.ElementTree as ET 


headers = {'Accept':'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 
'Accept-Encoding':'gzip, deflate', 
'Accept-Language':'en-US,en;q=0.8', 
'Cache-Control':'max-age=0', 
'Connection':'keep-alive', 
'Content-Length':'112', 
'Content-Type':'application/x-www-form-urlencoded', 
'User-Agent':"Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/43.0.2357.81 Safari/537.36",} 

xml = """<?xml version="1.0" encoding="UTF-8"?> 
      <!DOCTYPE protocol SYSTEM "https://www.paygate.co.za/payxml/payxml_v4.dtd"> 
       <protocol ver="4.0" pgid="10011013800" pwd="test"> 
        <authtx cref="ABCX1j64564" cname="Patel Sunny" cc="5200000000000015" exp="032022" budp="0" amt="10000" cur="ZAR" cvv="123" 
         rurl="http://localhost/pg_payxml_php_final.php" nurl="http://localhost/pg_payxml_php_notify.php" /> 
       </protocol> 
     """ 
headers = {'Content-Type': 'application/xml'} # set what your server accepts 

response = requests.post('https://www.paygate.co.za/payxml/process.trans', data=xml, headers=headers).text 

tree = ET.fromstring(response) 


for node in tree.iter('authrx'): 
    sdesc = node.attrib.get('sdesc') # STATUS MESSAGE 
    tid = node.attrib.get('tid') # TRANSACTION ID 
    cref = node.attrib.get('cref') # REFERENCE NO. like invoice_no or sale_order_no 
    auth = node.attrib.get('auth') 
    rdesc = node.attrib.get('rdesc') # Result Code description. 
    print sdesc, tid, cref 
Смежные вопросы