首页 文章

如何使用PHP在数组中返回此XML-RPC响应?

提问于
浏览
0

我正在尝试整理一个WordPress插件,我想通过XML-RPC获取所有类别(其他WordPress博客)的列表 . 我有以下代码,它看起来像目前为止:

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" } }

1 回答

  • 0

    你需要迭代你的数组:

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

相关问题