Search code examples
phpsoapwsdlstdclass

Turning a SOAP response into an STD object with PHP


I am learning about SOAP for a project and I now understand the basics of it. Can't believe I've never tried using this before. It's great. But anyway, to my problem.

I have managed to successfully print the response of my request to the web browser but I cannot seem to convert this response into an STD object.

I've been following a tutorial and adapting it to a different WDSL file to fully understand what I'm doing.

This is my PHP file.

<?

//////////////////////////////////////
//
//  ABOUT:      This file will send a request to the WSDL file and return a result in the browser window
//  AUTHOR:     Brad Bird
//  DATE:       07/02/2013
//
//////////////////////////////////////

// Setup the SOAP Client options
$wsdl = "http://www.mobilefish.com/services/web_service/countries.php?wsdl";
$options = array(
    "trace" => 1,
    "exception" => 0
);

// Creates new instance of the SOAP Client
$client = new SoapClient($wsdl, $options);

// Return a set of information using one function
$countryCode = "af";
$values = $client->countryInfoByIana($countryCode);

// Prints the details of the request and response to the browser
print "<h2>SOAP Details</h2>";
print "<pre>";
print "<h3>Request</h3> " . htmlspecialchars($client->__getLastRequest()) . "<br />";
print "<h3>Response</h3> " . htmlspecialchars($client->__getLastResponse());
print "</pre>";

// Prints the request in XML format
$xml = $values->countryInfoByIanaResponse;

print "<h2>stdClass Object</h2>";
print "<pre>";
print_r($xml);
print "</pre>";

And this WSDL file I'm trying to get request from is here. http://www.mobilefish.com/services/web_service/countries.php?wsdl

For some reason the STD Object section is showing nothing. Any ideas?


Solution

  • Well, $xml is not an object of stdClass, but null. $values however is. $values->countryInfoByIanaResponse does not exist. All you have in $values is

    : object(stdClass) = 
      ianacode: string = "af"
      countryname: string = "Afghanistan"
      latitude: double = 33.93911
      longitude: double = 67.709953
    

    Not sure what exactly what you are trying to do - maybe call a method on the $client rather than on your result? Also, check the PHP manual on error_reporting, you would then have stumbled across this Notice: Undefined property: stdClass::$countryInfoByIanaResponse in the print_r line.