【问题标题】:Converting PHP Curl request of PAYGATE API to python将 PAYGATE API 的 PHP Curl 请求转换为 python
【发布时间】:2015-10-25 03:44:53
【问题描述】:

我对 php 和 curl 不太熟悉,需要将一个高级 PHP cURL POST 请求转换为 python 等效项。

这是来自支付网关网站 paygate 的代码,我使用的是来自developer.paygate.co.za/ 的示例 php API。我试图转换成 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);

我知道 python 中的基本请求,但无法在该请求中传递属性,我也对在请求中传递 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 动词。

【问题讨论】:

    标签: php python url curl pycurl


    【解决方案1】:

    终于解决了,

    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
    

    【讨论】:

      猜你喜欢
      • 2020-05-29
      • 2017-05-25
      • 2017-08-03
      • 1970-01-01
      • 2023-03-26
      • 1970-01-01
      • 2017-04-12
      • 1970-01-01
      相关资源
      最近更新 更多