1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Problems consuming Webservice, returning NULL everytime

Discussion in 'PHP' started by ozwolverine, Dec 26, 2006.

  1. #1
    Hello everybody, I'm trying to do a webservice with php5 SOAP, after done the class, generated the wsdl file and coded the client, when I try to run it on a browser the function I call always returns NULL, why that? I tryed first to do a simple addition, but as this isn't working, I tryed just to return one of the parameters of the function but it isn't working either, what should I do? I'm using a generator to obtain the wsdl file

    This is my code:
    class with generator (example.php)
    <?php
    
    
    /**
     * this is a class
     */
    class Foo {
      /** The add function
       * @param int $x
       * @param int $y
       * @return int
       */
      function Add($x,$y) {
            if($x == null)
                    return 0;
            elseif($y == null)
                    return 1;
            else return 2;
      }
    }
    
    include '../WSDL_Gen.php';
    $wsdl = new WSDL_Gen('Foo', 'http://172.16.0.147:81/~jgonzal/WebserviceTest/wsdlTools/WSDL/Foo/Foo.php', 'http://172.16.0.147:81/~jgonzal/WebserviceTest/wsdlTools/WSDL/Foo/');
    print $wsdl->toXML();
    ?>
    
    PHP:

    The Foo class is like this, as you will see it is equals as the class into de wsdl generator file (Foo.php)

    
    <?php
    
    /**
     * this is a class
     */
    class Foo {
      /** The add function
       * @param int $x
       * @param int $y
       * @return int
       */
      function Add($x,$y) {
            if($x == null)
                    return 0;
            elseif($y == null)
                    return 1;
            else return 2;
      }
    }
    ?>
    
    PHP:

    This is the autogenerated wsdl file (wsdlFoo.wsdl):

    
    <?xml version="1.0" ?>
     <definitions xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://172.16.0.147:81/~jgonzal/WebserviceTest/wsdlTools/WSDL/Foo/" xmlns:soap-env="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" targetNamespace="http://172.16.0.147:81/~jgonzal/WebserviceTest/wsdlTools/WSDL/Foo/">
     <types>
     <xsd:schema>
     <xsd:complexType name="array">
     <xsd:complexContent>
     <xsd:restriction base="soapenc:array">
      <xsd:attribute ref="soapenc:arrayType" arrayType="tns:mixed[]" />
      </xsd:restriction>
      </xsd:complexContent>
      </xsd:complexType>
     <complexType name="mixed">
     <all>
      <element name="varString" type="xsd:string" />
      <element name="varInt" type="xsd:int" />
      <element name="varFloat" type="xsd:float" />
      <element name="varArray" type="tns:array" />
      </all>
      </complexType>
      </xsd:schema>
      </types>
     <message name="AddInput">
      <part name="x" type="xsd:int" />
      <part name="y" type="xsd:int" />
      </message>
     <message name="AddOutput">
      <part name="return" type="xsd:int" />
      </message>
     <portType name="FooPortType">
     <operation name="Add">
      <input message="tns:AddInput" />
      <output message="tns:AddOutput" />
      </operation>
      </portType>
     <binding name="FooBinding" type="tns:FooPortType">
      <soap-env:binding xmlns="http://schemas.xmlsoap.org/wsdl/soap/" style="rpc" transport="http://schemas.xmlsoap.org/soap/http" />
     <operation xmlns:default="http://schemas.xmlsoap.org/wsdl/soap/" name="Add">
     <input xmlns:default="http://schemas.xmlsoap.org/wsdl/soap/">
      <soap-env:body xmlns="http://schemas.xmlsoap.org/wsdl/soap/" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
      </input>
     <output xmlns:default="http://schemas.xmlsoap.org/wsdl/soap/">
      <soap-env:body xmlns="http://schemas.xmlsoap.org/wsdl/soap/" use="encoded" encodingStyle="http://schemas.xmlsoap.org/soap/encoding/" />
      </output>
      </operation>
      </binding>
     <service name="FooService">
     <port xmlns:default="http://schemas.xmlsoap.org/wsdl/soap/" name="FooPort" binding="tns:FooBinding">
      <soap-env:address xmlns="http://schemas.xmlsoap.org/wsdl/soap/" location="http://172.16.0.147:81/~jgonzal/WebserviceTest/wsdlTools/WSDL/Foo/Foo.php" />
      </port>
      </service>
    </definitions>
    
    HTML:

    This is my client code (clienteFoo.php)
    
    <?php
      $soap = new SoapClient('wsdlFoo.wsdl');
      try {
        var_dump($soap->__getFunctions());
        $result = $soap->Add(47, 11);
        //if( $result != null || $result != '')
            echo "Variable1:";
            var_dump($result);
            echo "Variable2:";
            echo "<h1>47 + 11 = $result.</h1>";
        //else
    //      echo "No se hizo la adicion";
      } catch (SoapFault $e) {
        echo "Error: {$e->faultstring}";
      }
    ?>
    
    PHP:
    Output in browser:

    array(1) { [0]=> string(23) "int Add(int $x, int $y)" } Variable1:NULL Variable2:
    47 + 11 = .
     
    ozwolverine, Dec 26, 2006 IP