'Consuming Chamilo PHP web services with python

Good afternoon,

I am starting with Python, and I am trying to create a script that will get me a Chamilo user through the webservices that are in PHP.

In php I do them through NuSoap without problems, but I don't know how to do it in Python.

PHP example:

$client = new nusoap_client($url . 'registration.soap.php?wsdl', true);
$client->xml_encoding = 'UTF-8';
$client->http_encoding = 'UTF-8';
$client->charencoding = 'UTF-8';

$client->soap_defencoding = 'UTF-8';

$soap_error = $client->getError();

if (!empty($soap_error)) {
    $error_message = 'Nusoap object creation failed: '.$soap_error;
    throw new Exception($error_message);
}
$client->setDebugLevel(10000);
$client->debug_flag = true;

$key = 'xxxxxxxxxxxxxxxxxxxxxxx';
        
$finalKey = sha1($myIp.$key) ;

$params = [
    'username' => (string) $email ,
    'secret_key' => $finalKey,
];

$result = $client->call('WSGetUserFromUsername', ['GetUserFromUsername' => $params]);
if(!empty($result['user_id'])){
    ...
}

In pyhon I don't know how to do it.

import hashlib
from requests import get, post

url = 'http://xxxxxxxxxx.com/main/webservices/';

# set the WSDL URL
wsdl_url = url + 'registration.soap.php?wsdl'

security_key = 'xxxxxxxxxxxxxxxx'

my_ip = get(url + 'testip.php').text

secret_key = hashlib.sha1((my_ip + security_key).encode(encoding='UTF-8', errors='strict')).hexdigest()

# Payload
payload = {
    'username': mail,  # the system field in the user profile (See Profiling)
    'secret_key': secret_key,
}

headers = {
    'Content-Type': 'text/xml; charset=utf-8',
    'SOAPAction': url + 'registration.soap.php?wsdl'
}

# Create the SOAP request
soap_request = ????

# Send the SOAP request
response = post(wsdl_url, data=soap_request, headers=headers)

# Parse the SOAP response
soap_response = response.content.decode('utf-8')
print(soap_response)

I don't know if this is how I should do it. How can I make the soap request?

Thank You.



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source