Page 1 of 1

Array returned by SoapClient

Posted: Mon May 24, 2010 5:44 am
by cluster28
Hi every body, i´m new in the Forum and i´m from Spain.

I have a problem with a array returned by a WebService throw SoapClient. It returns an array that one of the keys is an string value "1". So PHP can not access to the following array inside.

For instance:

I receive this result doing

Code: Select all


$soap = new soapclient('wsdl',array('location'=>'https://www.domain.com/servlet/APIv3','trace'=>true,'exceptions'=>false));
$array = $soap->__soapCall('method', $data);
["recsonpage"]=> string(1) "1"
["recsindb"]=> string(1) "1"
["1"]=> array(18) {
["thawtecertstat"]=> string(2) "NA"
["websiteid"]=> string(8) "16494076"
["domainstat"]=> string(6) "Active"

If i would to access to "domainstat" i will write

Code: Select all

$array["1"]["domainstat"];
But key ["1"] it arrives in string type and PHP con not read numerical keys like string. Itself modify the string to int if we declare the array manually. And search for int key, like indexed array, but in this case is empty.

That´s only happen wirh arrays returned by SOAP.

Thank you.

Re: Array returned by SoapClient

Posted: Mon May 24, 2010 1:17 pm
by Christopher
Maybe try:

Code: Select all

$array[$array["recsonpage"]]["domainstat"];

Re: Array returned by SoapClient

Posted: Tue May 25, 2010 1:46 am
by cluster28
It should be

Code: Select all

$array[$array["1"]]["domainstat"];
but it doesn´t works.

Thak you anyway.

Re: Array returned by SoapClient

Posted: Tue May 25, 2010 3:03 am
by Christopher
cluster28 wrote:It should be

Code: Select all

$array[$array["1"]]["domainstat"];
but it doesn´t works.

Thak you anyway.
No ... it shouldn't be that. In you example $array["recsonpage"] is a string with value "1". I was suggesting using that.

Re: Array returned by SoapClient

Posted: Tue May 25, 2010 3:40 am
by cluster28
I think i explained wrong. The array i had is (there are more fields but it´s reduced):

Code: Select all

array(3)
{
["recsonpage"]=> "1",
["recsindb"]=> "1",
["1"]=> array(3) {["thawtecertstat"]=> string(2) "NA", ["websiteid"]=> string(8) "16494076", ["domainstat"]=> string(6) "Active" }
}
If i would access to domainstat i would do that: $array["1"]["domainstat"] Correct?

Bur ["1"] is not a indexed key. Is a string type key (because Soap returns that format), and PHP can´t read it like a string. PHP think that ["1"] is index 1 and in $array is NULL.

Re: Array returned by SoapClient

Posted: Tue May 25, 2010 9:26 am
by AbraCadaver
I can't reproduce this, but it seems to be a bug in several libraries in different versions of PHP, although I didn't find one mentioned for soapclient. You might try upgrading PHP. If that doesn't help or you can't upgrade, you may do something like this (not tested as I can't define a numeric string key):

Code: Select all

foreach($array as $k => $v) {
   if(is_numeric($k)) {
      $result[(int)$k] = $v;
   } else {
      $result[$k] = $v;
   }
}
Actually, PHP shouldn't let you build an array with a numeric string key, so this may work:

Code: Select all

foreach($array as $k => $v) {
      $result[$k] = $v;
}
You might also try this to see if it fixes it:

Code: Select all

ksort($array);

Re: Array returned by SoapClient

Posted: Tue May 25, 2010 9:39 am
by cluster28
I tried with ksort and neither works.

foreach method works perfectly bur i thought there would be better solution.

I will look about upgrade.

Thank you.

Re: Array returned by SoapClient

Posted: Tue May 25, 2010 10:11 am
by Weirdan
AbraCadaver wrote: (not tested as I can't define a numeric string key)
I was able to build such array using json_decode and array cast:

Code: Select all

<?php
    $a = '{"1":"asd"}';
    var_dump(phpversion(), (array)json_decode($a));

Code: Select all

weirdan@virtual-debian: ~/tests$ php q.php
string(8) "5.2.12-2"
array(1) {
  ["1"]=>
  string(3) "asd"
}
weirdan@virtual-debian: ~/tests$ sudo update-alternatives --set php /usr/local/php/bin/php
update-alternatives: using /usr/local/php/bin/php to provide /usr/bin/php (php) in manual mode.
weirdan@virtual-debian: ~/tests$ php q.php
string(5) "5.3.1"
array(1) {
  ["1"]=>
  string(3) "asd"
}
weirdan@virtual-debian: ~/tests$

Re: Array returned by SoapClient

Posted: Tue May 25, 2010 11:20 am
by AbraCadaver
So these are hacks and maybe there is a better way to return your soap response, but here are several ways:

Code: Select all

$array = array_combine(array_keys($array), array_values($array));
echo $array[1];
Or this (but then the keys are renumbered starting at 0):

Code: Select all

shuffle($array);
echo $array[0];
Or use an object

Code: Select all

$object = (object) $soap->__soapCall('method', $data);
echo $object->{"1"};

Re: Array returned by SoapClient

Posted: Wed May 26, 2010 2:01 am
by cluster28
AbraCadaver wrote:So these are hacks and maybe there is a better way to return your soap response, but here are several ways:

Code: Select all

$array = array_combine(array_keys($array), array_values($array));
echo $array[1];
This one is very simply and works fine.

I upgraded soapclient and the problems persists.