2012-10-05 46 views
0

我試圖把一個WordPress插件放在一起,我想通過XML-RPC獲取所有類別(其他WordPress博客)的列表。我有以下的代碼,它看起來像它的工作原理至今:如何使用PHP在數組中返回此XML-RPC響應?

function get_categories($rpcurl,$username,$password){ 
    $rpcurl2 = $rpcurl."/xmlrpc.php"; 

    $params = array(0,$username,$password,true); 
    $request = xmlrpc_encode_request('metaWeblog.getCategories',$params); 
    $ch = curl_init(); 

    curl_setopt($ch, CURLOPT_URL, $rpcurl2); 
    curl_setopt($ch, CURLOPT_HEADER, false); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-Type: text/xml")); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_TIMEOUT, 10); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $request); 

    $results = curl_exec($ch); 
    $res = xmlrpc_decode($results); 
    curl_close($ch); 
    return $res; 
} 

如果我使用$res我碰到下面的字符串作爲迴應:Array

如果我使用$results然後我得到:

categoryId17 parentId0 descriptionTest categoryDescription categoryNameTest 
htmlUrlhttp://test.yoursite.com/?cat=17 rssUrlhttp://test.yoursite.com/?feed=rss2&cat=17 
categoryId1 parentId0 descriptionUncategorized categoryDescription 
categoryNameUncategorized htmlUrlhttp://test.yoursite.com/?cat=1 
rssUrlhttp://test.yoursite.com/?feed=rss2&cat=1 

在這種情況下,我需要在description所以UncategorizedTest之間抽出名稱。

這是我第一次用PHP編碼。我得到這些結果通過呼應他們到頁面,所以不知道他們是否在該過程中更改或不...

順便說一句,我修改了上述代碼從一個張貼到一個WordPress博客遠程,所以也許我沒有正確設置一些選項?

隨着var_dump($res)我得到:

array(2) { [0]=> array(7) { ["categoryId"]=> string(2) "17" ["parentId"]=> string(1) 
"0" ["description"]=> string(4) "Test" ["categoryDescription"]=> string(0) "" 
["categoryName"]=> string(4) "Test" ["htmlUrl"]=> string(40) 
"http://test.youreventwebsite.com/?cat=17" ["rssUrl"]=> string(54) 
"http://test.youreventwebsite.com/?feed=rss2&cat=17" } [1]=> array(7) { 
["categoryId"]=> string(1) "1" ["parentId"]=> string(1) "0" ["description"]=> 
string(13) "Uncategorized" ["categoryDescription"]=> string(0) "" ["categoryName"]=> 
string(13) "Uncategorized" ["htmlUrl"]=> string(39) "http://test.youreventwebsite.com/?cat=1" 
["rssUrl"]=> string(53) "http://test.youreventwebsite.com/?feed=rss2&cat=1" } } 
+0

是您迴應不要以爲是''中的XML-RPC' XML' ??? – Baba

+0

對不起巴巴,不知道你的意思是... –

+0

你用var_dump($ res)得到了什麼? –

回答

0

您需要遍歷您的數組:

foreach($res as $item) { 
    echo $item['description'] . $item['categoryName'] . $item['htmlUrl']; //etc... 

}