محمد خليل
محمد خليل

Reputation: 137

Soap data gives Fatal error: Cannot use object of type stdClass as array in

I am retriveing data from a soap api using php. but unable to get the data in a array. Every time it gives me error

Fatal error: Cannot use object of type stdClass as array in

stdClass Object (

[item] => Array
    (
        [0] => stdClass Object
            (
                [date] => 2008-07-17T01:23:06Z
                [directory] => 1
                [downloadCount] => 0
                [downloadLink] => http://www.example.com/folder/8ISxjbEs/_online.html
                [empty] => 
                [id] => 8290268
                [md5] => 
                [name] => 
                [parentId] => -1
                [removed] => 
                [shared] => 
                [size] => 17
                [version] => 0
            )

        [1] => stdClass Object
            (
                [date] => 2009-11-03T23:03:15Z
                [directory] => 
                [downloadCount] => 5
                [downloadLink] => http://www.example.com/file/mIofv-vJ/MASTER-ACCOUNTS_3_Nov_2009.html
                [empty] => 
                [id] => 146103085
                [md5] => b073b9573227843e25d19e0e9e60ce80
                [name] => MASTER-ACCOUNTS 3 Nov 2009.zip
                [parentId] => 8290268
                [removed] => 
                [shared] => 
                [size] => 3401447
                [version] => 0
            )
  ) )

Ok i am using 4shared API.

//User credentials on 4shared

$user_login = "[email protected]";
$user_password = "password";

$client = new SoapClient("https://api.4shared.com/jax2/DesktopApp?wsdl", array(
"cache_wsdl" => WSDL_CACHE_DISK,
"trace" => 1, 
"exceptions" => 0
)
); 

$client->yourFunction();
//Getting list of all folders 
echo "<pre>";

$getAllItems  = $client->getAllItems ($user_login, $user_password);

print_r  ($getAllItems);

this code is printing the above stdClass object. but i am unable to convert it into array.

Upvotes: 1

Views: 6934

Answers (2)

kaninabu
kaninabu

Reputation: 94

Have you tried converting using the get_object_vars(); function?

<?php

function objectToArray($d) {
    if (is_object($d)) {
        // Gets the properties of the given object
        // with get_object_vars function
        $d = get_object_vars($d);
    }

    if (is_array($d)) {
        /*
        * Return array converted to object
        * Using __FUNCTION__ (Magic constant)
        * for recursive call
        */
        return array_map(__FUNCTION__, $d);
    }
    else {
        // Return array
        return $d;
    }
}

?>

*I know it's kinda late to answer but i came across this question while looking for a solution for my problem so here's my 2 cents :)

http://www.if-not-true-then-false.com/2009/php-tip-convert-stdclass-object-to-multidimensional-array-and-convert-multidimensional-array-to-stdclass-object/

Upvotes: 1

0xd
0xd

Reputation: 1901

You can try to cast that object as an Array:

class myClass {
...
}
$myobj = new myClass();
$myArrObj = (Array) $myobj;

Or you can try to iterate through this object and push all elements to an new array using get_object_vars

Upvotes: 1

Related Questions