2011-10-21 50 views
1

我從使用PHP的soap api回收數據。但無法獲取數組中的數據。每次它給了我錯誤肥皂數據給出致命錯誤:不能使用類型爲stdClass的對象作爲數組

致命錯誤時間:無法在

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

確定使用類型爲stdClass的對象作爲數組我使用4shared API。

上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); 

此代碼//用戶憑證是印刷上述stdClass的對象。但我無法將其轉換爲數組。

+0

請後使用的代碼檢索並存儲SOAP響應。 –

+0

我已添加更多詳情,請現在查看。 –

回答

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/

1

您可以嘗試施放該物體爲一個數組:

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

或者你可以嘗試通過這個對象上進行迭代,並使用get_object_vars

相關問題