肥皂数据给出致命错误:不能使用stdClass类型的对象作为数组

时间:2011-10-21 11:10:25

标签: php api soap

我正在使用php从soap api中检索数据。但无法获取数组中的数据。每次它给我错误

致命错误:无法使用stdClass类型的对象作为数组

  

stdClass对象(

[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
            )
  ) )

好的我正在使用4shared API。

// 4shared上的用户凭据

$user_login = "email_123@hotmail.com";
$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);

此代码正在打印上面的stdClass对象。但我无法将其转换为数组。

2 个答案:

答案 0 :(得分:1)

您可以尝试将该对象强制转换为数组:

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

或者您可以尝试迭代此对象并使用get_object_vars将所有元素推送到新数组

答案 1 :(得分:1)

您是否尝试使用get_object_vars()进行转换;功能

<?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;
    }
}

?>

*我知道回答有点迟,但我在寻找问题的解决方案时遇到了这个问题所以这是我的2美分:)

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