Skip to content
Advertisement

Difference between PHP and python post request?

i tried to connect to API. All auth I have good done, results are right. But response always take error code 2009 -> request is empty.

PHP that works

$login = 'tester';
$wpass = 'fdsaasdf';
$auth = sha1($login.sha1($wpass).date('H', time()));
$command = 'ping';
$cltrid = 'test_req_1';

$request = '<?xml version="1.0" encoding="UTF-8"?>
<request>
 <user>'.$login.'</user>
 <auth>'.$auth.'</auth>
 <command>'.$command.'</command>
 <clTRID>'.$cltrid.'</clTRID>
</request>';

$url = 'https://api.wedos.com/wapi/xml';

$post = 'request='.urlencode($request);

$ch = curl_init();

curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_POST,true);
curl_setopt($ch,CURLOPT_POSTFIELDS,$post);

curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);

curl_setopt($ch,CURLOPT_TIMEOUT,100);

$res = curl_exec($ch);

echo '<pre>'.htmlspecialchars(print_r($res, true)).'</pre>';

Python code -> reverse reverse engineering + documention

import requests
from hashlib import sha1
import datetime
import json

hour=datetime.datetime.now().hour
user = 'tester'
url='https://api.wedos.com/wapi/xml'
command="ping"
cltrid="test_req_1"

authorization=sha1()
authorization.update(b"fdsaasdf")
auth=sha1()
auth_code=user+authorization.hexdigest()+str(hour)
auth_code=bytes(auth_code,"utf-8")
auth.update(auth_code)
data='<?xml version="1.0" encoding="UTF-8"?><request><user>'+user+'</user><auth>'+auth.hexdigest()+'</auth><command>'+command+'</command><clTRID>'+cltrid+'</clTRID></request>'
response = requests.post(url, data=data,header={'Content-Type': 'application/xml'})
print(response.json())

Can anyone help me with this specific problem?

Advertisement

Answer

To resemble the php code the data attribute must be formed as following:

data = {"request": '<?xml version="1.0" encoding="UTF-8"?><request><user>'+user+'</user><auth>'+auth.hexdigest()+'</auth><command>'+command+'</command><clTRID>'+cltrid+'</clTRID></request>'}

that way the request should be the same as the PHP-Code one

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement