Search code examples
phppythonarrayswsdlws

Send an array from Python to PHP


I am trying to consume with Python a WSDL created in PHP.

Fragment of the Service:

        require_once('lib/nusoap.php');
        date_default_timezone_set('America/Mexico_City');
        function Sum($numbers){
            return array_sum($numbers);
        }
        ...

Client:

from suds.client import Client

    def wsarchivo():
        url = "http://localhost/PracticeSumArray/server.php?wsdl"
        client = Client(url)
        res = client.service.Sum([1,2,3])
        print(res)

    wsarchivo()

But when running it does not work to send the parameter in this way, unlike doing it with a client in PHP that the way to send the parameter would be 'numbers' => array (1, 2, 3) and works correctly .


Solution

  • I could solve my problem, I leave it here in case someone else is useful, the first mistake I made was to declare the input parameter as a whole xsd:int, so what I did was declare it as SOAP-ENC:Array on the server in PHP:

    $server->register(
            'Suma',
            array('numbers' => 'SOAP-ENC:Array'),
            array('return' => 'xsd:int'),
            $ns
        );
    

    Now in the Python client I used the factory.create() method of the suds client to declare a variable with the type SOAP-ENC:Array

    from suds.client import Client
    
    def wsarchivo():
        client = Client("http://localhost/PracticaSumaArray/server.php?wsdl")
        request = client.factory.create('SOAP-ENC:Array')
        request.numbers = [1,2,3]
        res = client.service.Suma(request)
        print(res)
    
    wsarchivo()
    

    When executing:

    Output:
    6