web services - PHP SOAP call issue using the SoapClient class

569

I am badly stuck in creating a client to consume this WSDL. (I am a novice when it comes to consuming webservices in PHP.) (For confidentiality reasons, I had to create a replica XML. I hope you understand.)

http://brique.in/wsdl.xml

I use the following code to print out the functions and the types:

$client = new SoapClient("http://brique.in/wsdl.xml");
var_dump($client->__getFunctions()); 
var_dump($client->__getTypes()); 

I am trying to write php code to call the method inwardProcess. It accepts as an input, an XML file. I tried the following:

$xmlInput = htmlentities('<XML><Refno>H9999999</Refno><Type>getDetails</Type><UserID>BO</UserID></XML>');
$result = $client->__soapCall("inwardProcess", array($xmlInput));

It didn't work. After looking at the WSDL specifications, I also tried,

$xmlInput = htmlentities('<XML><Refno>H9999999</Refno><Type>getDetails</Type><UserID>BO</UserID></XML>');
class inwardProcess {
    function inwardProcess($xmlInput) 
    {
        $this->xmlInput = $xmlInput;
    }
}
$inwardProcess = new inwardProcess($xmlInput);
$webservice = new SoapClient($url, $soap_options);
echo "Attempting Inward<br/>";
try {
    var_dump($webservice->__getTypes()); 
    //I also tried passing just $inwardProcess Object in place of array($inwardProcess)
    $result = $webservice->__soapCall("inwardProcess", array($inwardProcess));
    var_dump($result); 
} catch (SOAPFault $f) {
    echo "SOAPFault".$f;
}

I keep getting the error

Server was unable to process request. ---> Object reference not set to an instance of an object

Somehow I am not able to work it out. Any help would be highly appreciated since I am on a deadline.

568

Answer

Solution:

When structures are as complex as:

<s:element name="inwardProcess">
    <s:complexType>
        <s:sequence>
            <s:element minOccurs="0" maxOccurs="1" name="xmlInput">
                <s:complexType mixed="true">
                    <s:sequence>
                        <s:any/>
                    </s:sequence>
                </s:complexType>
            </s:element>
        </s:sequence>
    </s:complexType>
</s:element>

and response like

<s:element name="inwardProcessResponse">
    <s:complexType>
        <s:sequence>
            <s:element minOccurs="0" maxOccurs="1" name="inwardProcessResult">
                <s:complexType mixed="true">
                    <s:sequence>
                        <s:any/>
                    </s:sequence>
                </s:complexType>
            </s:element>
        </s:sequence>
    </s:complexType>
</s:element>

You have to create a class for each structure. In this case, it would be:

class xmlInput
{
    public $any = null;
    public function __construct($any)
    {
        $this->any = $any;
    }
}

class inwardProcess
{
    public $xmlInput = null;
    public function __construct($xmlInput)
    {
        $this->xmlInput = $xmlInput;
    }
}
class inwardProcessResponse
{
    public $inwardProcessResult = null;
    public function __construct($inwardProcessResult)
    {
        $this->inwardProcessResult = $inwardProcessResult;
    }
}

and finally call ...

$xmlInput = new xmlInput('<XML><Refno>H9999999</Refno><Type>getDetails</Type><UserID>BO</UserID></XML>');
$inwardProcess = new inwardProcess($xmlInput);
$soap_options = array(
    'trace'       => 1,     // traces let us look at the actual SOAP messages later
    'exceptions'  => 1 );
$url = "<WSDL URL>";
$client = new SoapClient($url, $soap_options);
try {
    $result = $client->__soapCall("inwardProcess", array($inwardProcess));
    echo htmlentities($result->inwardProcessResult->any);
} catch (SOAPFault $f) {
    echo "-1";
}

Worked!

People are also looking for solutions to the problem: php - "echo" session and logout in the same line

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.