问题 我正在使用 cURL 和 PHP 通过 XML-RPC(wp.getCategories 或 metaWeblog.getCategories)从 Wordpress 站点检索类别列表,但我无法解析响应,因为它看起来非常复杂。
我的代码应该解析响应并创建一个简单的下拉菜单:
这不应该是实际的工作代码,而是我如何想象它的工作方式:
$xml = simplexml_load_string($categories);
$select_string = "<select name=\"cats\">";
foreach ($xml as $category) {
$select_string .= "<option value=\"" . $category->value . "\">" . $category->name . "</option>";
}
$select_string .= "</select>";
我试图解析的响应是(这是 simplexml_load_string 输出的):
SimpleXMLElement Object
(
[param] => SimpleXMLElement Object
(
[value] => SimpleXMLElement Object
(
[array] => SimpleXMLElement Object
(
[data] => SimpleXMLElement Object
(
[value] => Array
(
[0] => SimpleXMLElement Object
(
[struct] => SimpleXMLElement Object
(
[member] => Array
(
[0] => SimpleXMLElement Object
(
[name] => categoryId
[value] => SimpleXMLElement Object
(
[string] => 4
)
)
[1] => SimpleXMLElement Object
(
[name] => parentId
[value] => SimpleXMLElement Object
(
[string] => 0
)
)
[2] => SimpleXMLElement Object
(
[name] => description
[value] => SimpleXMLElement Object
(
[string] => News
)
)
[3] => SimpleXMLElement Object
(
[name] => categoryDescription
[value] => SimpleXMLElement Object
(
[string] => SimpleXMLElement Object
(
)
)
)
[4] => SimpleXMLElement Object
(
[name] => categoryName
[value] => SimpleXMLElement Object
(
[string] => News
)
)
[5] => SimpleXMLElement Object
(
[name] => htmlUrl
[value] => SimpleXMLElement Object
(
[string] => http://lubranochiropractic.com/category/news/
)
)
[6] => SimpleXMLElement Object
(
[name] => rssUrl
[value] => SimpleXMLElement Object
(
[string] => http://lubranochiropractic.com/category/news/feed/
)
)
)
)
)
[1] => SimpleXMLElement Object
(
[struct] => SimpleXMLElement Object
(
[member] => Array
(
[0] => SimpleXMLElement Object
(
[name] => categoryId
[value] => SimpleXMLElement Object
(
[string] => 5
)
)
[1] => SimpleXMLElement Object
(
[name] => parentId
[value] => SimpleXMLElement Object
(
[string] => 4
)
)
[2] => SimpleXMLElement Object
(
[name] => description
[value] => SimpleXMLElement Object
(
[string] => Testimonials
)
)
[3] => SimpleXMLElement Object
(
[name] => categoryDescription
[value] => SimpleXMLElement Object
(
[string] => SimpleXMLElement Object
(
)
)
)
[4] => SimpleXMLElement Object
(
[name] => categoryName
[value] => SimpleXMLElement Object
(
[string] => Testimonials
)
)
[5] => SimpleXMLElement Object
(
[name] => htmlUrl
[value] => SimpleXMLElement Object
(
[string] => http://lubranochiropractic.com/category/news/testimonials/
)
)
[6] => SimpleXMLElement Object
(
[name] => rssUrl
[value] => SimpleXMLElement Object
(
[string] => http://lubranochiropractic.com/category/news/testimonials/feed/
)
)
)
)
)
[2] => SimpleXMLElement Object
(
[struct] => SimpleXMLElement Object
(
[member] => Array
(
[0] => SimpleXMLElement Object
(
[name] => categoryId
[value] => SimpleXMLElement Object
(
[string] => 1
)
)
[1] => SimpleXMLElement Object
(
[name] => parentId
[value] => SimpleXMLElement Object
(
[string] => 0
)
)
[2] => SimpleXMLElement Object
(
[name] => description
[value] => SimpleXMLElement Object
(
[string] => Uncategorized
)
)
[3] => SimpleXMLElement Object
(
[name] => categoryDescription
[value] => SimpleXMLElement Object
(
[string] => SimpleXMLElement Object
(
)
)
)
[4] => SimpleXMLElement Object
(
[name] => categoryName
[value] => SimpleXMLElement Object
(
[string] => Uncategorized
)
)
[5] => SimpleXMLElement Object
(
[name] => htmlUrl
[value] => SimpleXMLElement Object
(
[string] => http://lubranochiropractic.com/category/uncategorized/
)
)
[6] => SimpleXMLElement Object
(
[name] => rssUrl
[value] => SimpleXMLElement Object
(
[string] => http://lubranochiropractic.com/category/uncategorized/feed/
)
)
)
)
)
)
)
)
)
)
)
我试图在 foreach 语句中使用 this 遍历成员数组,但它不起作用:
$xml->param->value->array->data->value->struct->member;
有谁知道在这里轻松解析数据的简单方法?
实际 XML:
<!--?xml version="1.0"?-->
<methodresponse>
<params>
<param>
<value>
<array><data>
<value><struct>
<member><name>categoryId</name><value><string>4</string></value></member>
<member><name>parentId</name><value><string>0</string></value></member>
<member><name>description</name><value><string>News</string></value></member>
<member><name>categoryDescription</name><value><string></string></value></member>
<member><name>categoryName</name><value><string>News</string></value></member>
<member><name>htmlUrl</name><value><string>http://lubranochiropractic.com/category/news/</string></value></member>
<member><name>rssUrl</name><value><string>http://lubranochiropractic.com/category/news/feed/</string></value></member>
</struct></value>
<value><struct>
<member><name>categoryId</name><value><string>5</string></value></member>
<member><name>parentId</name><value><string>4</string></value></member>
<member><name>description</name><value><string>Testimonials</string></value></member>
<member><name>categoryDescription</name><value><string></string></value></member>
<member><name>categoryName</name><value><string>Testimonials</string></value></member>
<member><name>htmlUrl</name><value><string>http://lubranochiropractic.com/category/news/testimonials/</string></value></member>
<member><name>rssUrl</name><value><string>http://lubranochiropractic.com/category/news/testimonials/feed/</string></value></member>
</struct></value>
<value><struct>
<member><name>categoryId</name><value><string>1</string></value></member>
<member><name>parentId</name><value><string>0</string></value></member>
<member><name>description</name><value><string>Uncategorized</string></value></member>
<member><name>categoryDescription</name><value><string></string></value></member>
<member><name>categoryName</name><value><string>Uncategorized</string></value></member>
<member><name>htmlUrl</name><value><string>http://lubranochiropractic.com/category/uncategorized/</string></value></member>
<member><name>rssUrl</name><value><string>http://lubranochiropractic.com/category/uncategorized/feed/</string></value></member>
</struct></value>
</data></array>
</value>
</params>
</methodresponse>
谢谢。瑞克
最终解决方案(在 E_p 提供的帮助下) 这会根据来自 wp.getCategories 或 metaWeblog.getCategories 的 XML-RPC 响应构建类别的下拉菜单。
$categories = wpGetCategories($url, $username, $password);
$select_string = "<select name=\"cats\">";
$xml = simplexml_load_string($categories);
foreach ($xml->params->param->value->array->data->value as $dataSet) {
foreach ($dataSet->struct->member as $member) {
if ($member->name == "categoryId") $cat_id = $member->value->string;
if ($member->name == "description") $cat_name = $member->value->string;
}
$select_string .= "<option value=\"" . $cat_id . "\">" . $cat_name . "</option>";
}
$select_string .= "</select>";