0

I have a client server scenario where the type conversion did by the SoapClient class in PHP, cannot tell wether an empty array is associative or numeric, and so defaults to numeric.

All exposed functions use basic types, no classes.

An associative array such as array("something"=>123) gets converted to a map data type. However, when the same array is empty, such as array(), it gets converted to an array on the Ruby side. Type casting to object (object)array() will result in a struct data type on the Ruby side.

The argument is a bit more complex, not as simple as above:

array(
    "options"=>array(
        "plans"=>array(
            0=>array(
                "name"=>"abc",
                "product_options"=>array(
                    "optional_key_determines_associative_array_data_type"=>0,
                ),
            ),
        ),
    ),
);

If the array under "product_options" is empty, it gets converted to an array in Ruby, instead of a map. Once again, type casting to object in PHP results in a struct in Ruby.

What can I do on the PHP side to make empty "associative" arrays end up as maps on the Ruby side?

PHP 5.3.3, using SoapClient. Ruby 1.8.7, Rails 2.3.2 using Action Web Service.

7
  • I tried SOAP_USE_XSI_ARRAY_TYPE, it doesn't apply for the presented problem.
    – oxygen
    Commented Jun 13, 2012 at 11:23
  • Adding dummy array keys in PHP is not an accepted solution.
    – oxygen
    Commented Jun 13, 2012 at 11:37
  • An ugly hack would be to extend SoapClient & perform some operations in SoapClient::__doRequest (you get the xml as a string as it will be sent).
    – Wrikken
    Commented Jun 19, 2012 at 0:03
  • have you used php soap client . i dont know about ruby on rails but you can look at my question stackoverflow.com/questions/10511478/… . can we see the code you are using like in my question
    – Rinzler
    Commented Jun 19, 2012 at 6:12
  • @Wrikken By ugly, you mean playing around with XML would be very inneficient?
    – oxygen
    Commented Jun 19, 2012 at 10:30

3 Answers 3

2
+150

You can wrap your array in a SoapVar class with APACHE_MAP as encoding parameter. Like This:

array(
    "options"=>array(
        "plans"=>array(
            0=>array(
                "name"=>"abc",
                "product_options"=> new SoapVar(array(), APACHE_MAP),
            ),
        ),
    ),
);
1
  • Will test tomorow, and I'll report back. Thanks.
    – oxygen
    Commented Jun 20, 2012 at 15:17
0

Well, this is exactly what I mean: To overcome that problem you will need to change the logic in your scripts - not the PHP. As you can not define a PHP array to be associative you will need to modify the receiving script.

If it was me I would not send an empty array. Put a status field into the array. This could be a field counting the available products which in this case would count 0. You will have a more meaningful communication AND the array IS suddenly associative no matter what e.g

"product_options" => array ('products'=>0,'...'=>...) and so forth. 

What I say is you will need to change the logic, you can not change PHP.

Hope that helps,

Uwe

2
  • product_options is a key value pair configuration. It will stay this way, and I prefer to fix the problem at its root (the caller, PHP).
    – oxygen
    Commented Jun 20, 2012 at 20:53
  • BTW, the array posted is just an example. There actually exists a products numeric array, and product_options is per product. However, this has nothing to do with the more general problem with associative arrays. The RPC API I'm connecting to is comprised of lots of other functions.
    – oxygen
    Commented Jun 20, 2012 at 21:02
-1

I might be wrong here, but by my understanding:

I do believe what you are trying to achieve is not possible. An array (and we are talking array only, no objects) is an array. The structure given by the content makes an array associative or not.

An empty array is not associative.

1
  • I am aware of this situation, as I specifically pointed out in my request. This is practically what needs to be overcome (I didn't downvote).
    – oxygen
    Commented Jun 20, 2012 at 12:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.