Bart Van der Donck
Bart Van der Donck

Reputation: 41

PHP object iteration

I run the following PHP-code:

$client = new SoapClient("https://ec.europa.eu/taxation_customs/vies/checkVatService.wsdl");

var_dump($client->checkVat(array(
  'countryCode' => 'BE',
  'vatNumber' => '0861091467'
)));

foreach ($client as $key => $value) {
   echo $key . ' = ' . $value . "\n";
}

It procudes the following output on screen:

object(stdClass)#2 (6) {
  ["countryCode"]=>
  string(2) "BE"
  ["vatNumber"]=>
  string(10) "0861091467"
  ["requestDate"]=>
  string(16) "2021-03-10+01:00"
  ["valid"]=>
  bool(true)
  ["name"]=>
  string(8) "BV myKMO"
  ["address"]=>
  string(26) "Kapellebaan 55
2560 Nijlen"
}
_soap_version = 1
sdl = Resource id #2
httpsocket = Resource id #3
_use_proxy = 0
httpurl = Resource id #4

Then how can we store "BV myKMO" in a one-dimensional variable ? I'm looking for something like

$varname = $client->sdl->name;

Thank you,

Upvotes: 0

Views: 52

Answers (1)

ADyson
ADyson

Reputation: 61839

Your issue is that $client doesn't contain the data. It merely contains your SOAP client - i.e. it contains functions which enable you to request data from the server, but not the data which is obtained from executing those requests.

The result data you want is contained in the object returned by the call to the checkVat() function...but you're only feeding that to var_dump, you aren't retaining it for later use.

Try this instead:

$result = $client->checkVat(array( 'countryCode' => 'BE', 'vatNumber' => '0861091467' )); 
echo $result->name;

Upvotes: 1

Related Questions