Irfana
Irfana

Reputation: 231

Issue accessing XML data

I have connected to a web service and received data in XML file format. When I get a var_dump($response); I get this array:

object(stdClass)#2 (1) {
  ["GetBookInfoByISBN"]=>
  object(stdClass)#3 (14) {
    ["Success"]=>
    bool(true)
    ["ResponseText"]=>
    string(10) "Book Found"
    ["State"]=>
    string(2) "CA"
    ["GetNumber"]=>
    string(8) "1234"
    ["BookID"]=>
    int(4) "12"
  }
}

Now I am trying to get value of the BookID:

$soapclient = new SoapClient('http://wsf.com/BookWS/Book.asmx?WSDL');
$params = array('ISBN' => '1111');
$response = $soapclient->GetBookInfoByISBN($params);
var_dump($response);
<form>
    <p><?php echo $response['BookID']; ?></p>
</form>

I am getting this error Fatal error: Cannot use object of type stdClass as array which obviously means I am not accessing the data correctly.

I appreciate it any suggestion.

Upvotes: 2

Views: 31

Answers (1)

Shashank Shah
Shashank Shah

Reputation: 2167

StdClass object is accessed by using ->

You must access it using -> since its an object.

Change your code from:

echo $response['BookID'];

To:

echo $response->BookID;

OR

You can convert stdClass object to array like:

$array = (array)$stdClass;

Upvotes: 1

Related Questions