douhao2721 2012-10-05 09:07
浏览 45
已采纳

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

I'm trying to put together a WordPress plugin and I want to grab a list of all categories (of other WordPress blogs) via XML-RPC. I have the following code and it looks like it works so far:

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

If I use $res I get the following string as the response: Array

If I use $results then I get:

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

I need to pull out the names after description so Uncategorized and Test in this case.

It's my first time coding in PHP. I got these results by echoing them to the page, so not sure if they get changed in that process or not...

By the way I modified the above code from one that posts to a WordPress blog remotely so maybe I haven't set some of the options correctly?

With var_dump($res) I get:

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条回答 默认 最新

  • douguan3470 2012-10-05 09:36
    关注

    You need to iterate your array:

    foreach($res as $item) {
       echo $item['description'] . $item['categoryName'] . $item['htmlUrl']; //etc...
    
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化