【问题标题】:How to return this XML-RPC response in an array using PHP?如何使用 PHP 在数组中返回这个 XML-RPC 响应?
【发布时间】:2012-10-05 09:07:19
【问题描述】:

我正在尝试组合一个 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" } } 

【问题讨论】:

  • 你的回复不应该是XML in XML-RPC ???
  • 对不起,爸爸,不明白你的意思......
  • 你用 var_dump($res) 得到了什么?
  • @AurimasLičkus 我已经用回复更新了我上面的问题。

标签: php wordpress xml-rpc


【解决方案1】:

你需要迭代你的数组:

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

}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2020-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-27
    相关资源
    最近更新 更多