1

问题 我正在使用 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] =&gt; SimpleXMLElement Object
    (
        [value] =&gt; SimpleXMLElement Object
            (
                [array] =&gt; SimpleXMLElement Object
                    (
                        [data] =&gt; SimpleXMLElement Object
                            (
                                [value] =&gt; Array
                                    (
                                        [0] =&gt; SimpleXMLElement Object
                                            (
                                                [struct] =&gt; SimpleXMLElement Object
                                                    (
                                                        [member] =&gt; Array
                                                            (
                                                                [0] =&gt; SimpleXMLElement Object
                                                                    (
                                                                        [name] =&gt; categoryId
                                                                        [value] =&gt; SimpleXMLElement Object
                                                                            (
                                                                                [string] =&gt; 4
                                                                            )

                                                                    )

                                                                [1] =&gt; SimpleXMLElement Object
                                                                    (
                                                                        [name] =&gt; parentId
                                                                        [value] =&gt; SimpleXMLElement Object
                                                                            (
                                                                                [string] =&gt; 0
                                                                            )

                                                                    )

                                                                [2] =&gt; SimpleXMLElement Object
                                                                    (
                                                                        [name] =&gt; description
                                                                        [value] =&gt; SimpleXMLElement Object
                                                                            (
                                                                                [string] =&gt; News
                                                                            )

                                                                    )

                                                                [3] =&gt; SimpleXMLElement Object
                                                                    (
                                                                        [name] =&gt; categoryDescription
                                                                        [value] =&gt; SimpleXMLElement Object
                                                                            (
                                                                                [string] =&gt; SimpleXMLElement Object
                                                                                    (
                                                                                    )

                                                                            )

                                                                    )

                                                                [4] =&gt; SimpleXMLElement Object
                                                                    (
                                                                        [name] =&gt; categoryName
                                                                        [value] =&gt; SimpleXMLElement Object
                                                                            (
                                                                                [string] =&gt; News
                                                                            )

                                                                    )

                                                                [5] =&gt; SimpleXMLElement Object
                                                                    (
                                                                        [name] =&gt; htmlUrl
                                                                        [value] =&gt; SimpleXMLElement Object
                                                                            (
                                                                                [string] =&gt; http://lubranochiropractic.com/category/news/
                                                                            )

                                                                    )

                                                                [6] =&gt; SimpleXMLElement Object
                                                                    (
                                                                        [name] =&gt; rssUrl
                                                                        [value] =&gt; SimpleXMLElement Object
                                                                            (
                                                                                [string] =&gt; http://lubranochiropractic.com/category/news/feed/
                                                                            )

                                                                    )

                                                            )

                                                    )

                                            )

                                        [1] =&gt; SimpleXMLElement Object
                                            (
                                                [struct] =&gt; SimpleXMLElement Object
                                                    (
                                                        [member] =&gt; Array
                                                            (
                                                                [0] =&gt; SimpleXMLElement Object
                                                                    (
                                                                        [name] =&gt; categoryId
                                                                        [value] =&gt; SimpleXMLElement Object
                                                                            (
                                                                                [string] =&gt; 5
                                                                            )

                                                                    )

                                                                [1] =&gt; SimpleXMLElement Object
                                                                    (
                                                                        [name] =&gt; parentId
                                                                        [value] =&gt; SimpleXMLElement Object
                                                                            (
                                                                                [string] =&gt; 4
                                                                            )

                                                                    )

                                                                [2] =&gt; SimpleXMLElement Object
                                                                    (
                                                                        [name] =&gt; description
                                                                        [value] =&gt; SimpleXMLElement Object
                                                                            (
                                                                                [string] =&gt; Testimonials
                                                                            )

                                                                    )

                                                                [3] =&gt; SimpleXMLElement Object
                                                                    (
                                                                        [name] =&gt; categoryDescription
                                                                        [value] =&gt; SimpleXMLElement Object
                                                                            (
                                                                                [string] =&gt; SimpleXMLElement Object
                                                                                    (
                                                                                    )

                                                                            )

                                                                    )

                                                                [4] =&gt; SimpleXMLElement Object
                                                                    (
                                                                        [name] =&gt; categoryName
                                                                        [value] =&gt; SimpleXMLElement Object
                                                                            (
                                                                                [string] =&gt; Testimonials
                                                                            )

                                                                    )

                                                                [5] =&gt; SimpleXMLElement Object
                                                                    (
                                                                        [name] =&gt; htmlUrl
                                                                        [value] =&gt; SimpleXMLElement Object
                                                                            (
                                                                                [string] =&gt; http://lubranochiropractic.com/category/news/testimonials/
                                                                            )

                                                                    )

                                                                [6] =&gt; SimpleXMLElement Object
                                                                    (
                                                                        [name] =&gt; rssUrl
                                                                        [value] =&gt; SimpleXMLElement Object
                                                                            (
                                                                                [string] =&gt; http://lubranochiropractic.com/category/news/testimonials/feed/
                                                                            )

                                                                    )

                                                            )

                                                    )

                                            )

                                        [2] =&gt; SimpleXMLElement Object
                                            (
                                                [struct] =&gt; SimpleXMLElement Object
                                                    (
                                                        [member] =&gt; Array
                                                            (
                                                                [0] =&gt; SimpleXMLElement Object
                                                                    (
                                                                        [name] =&gt; categoryId
                                                                        [value] =&gt; SimpleXMLElement Object
                                                                            (
                                                                                [string] =&gt; 1
                                                                            )

                                                                    )

                                                                [1] =&gt; SimpleXMLElement Object
                                                                    (
                                                                        [name] =&gt; parentId
                                                                        [value] =&gt; SimpleXMLElement Object
                                                                            (
                                                                                [string] =&gt; 0
                                                                            )

                                                                    )

                                                                [2] =&gt; SimpleXMLElement Object
                                                                    (
                                                                        [name] =&gt; description
                                                                        [value] =&gt; SimpleXMLElement Object
                                                                            (
                                                                                [string] =&gt; Uncategorized
                                                                            )

                                                                    )

                                                                [3] =&gt; SimpleXMLElement Object
                                                                    (
                                                                        [name] =&gt; categoryDescription
                                                                        [value] =&gt; SimpleXMLElement Object
                                                                            (
                                                                                [string] =&gt; SimpleXMLElement Object
                                                                                    (
                                                                                    )

                                                                            )

                                                                    )

                                                                [4] =&gt; SimpleXMLElement Object
                                                                    (
                                                                        [name] =&gt; categoryName
                                                                        [value] =&gt; SimpleXMLElement Object
                                                                            (
                                                                                [string] =&gt; Uncategorized
                                                                            )

                                                                    )

                                                                [5] =&gt; SimpleXMLElement Object
                                                                    (
                                                                        [name] =&gt; htmlUrl
                                                                        [value] =&gt; SimpleXMLElement Object
                                                                            (
                                                                                [string] =&gt; http://lubranochiropractic.com/category/uncategorized/
                                                                            )

                                                                    )

                                                                [6] =&gt; SimpleXMLElement Object
                                                                    (
                                                                        [name] =&gt; rssUrl
                                                                        [value] =&gt; SimpleXMLElement Object
                                                                            (
                                                                                [string] =&gt; 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>";
4

1 回答 1

0

试过:

<?php 

    $str = "<a><tests><test>1</test><test>2</test><test>3</test><test>4</test></tests></a>";
    $xml = simplexml_load_string($str);

    foreach ($xml->tests->test as $test) {
        echo "Test #: " . $test . PHP_EOL;                                                                                                                                                                                                    
    }

返回:

Test #: 1
Test #: 2
Test #: 3
Test #: 4 

所以在你的情况下foreach ($xml->param->value->array->data->value->struct->member as $member)应该工作

你能提供你解析的xml吗?

尝试使用您的 xml(它有语法错误):

$xml = simplexml_load_string('your xml here');
foreach ($xml->params->param->value->array->data->value as $dataSet) {
    foreach ($dataSet->struct->member as $member) {
        echo 'Name: ' . $member->name  . PHP_EOL;
        echo 'Value: ' . $member->value->string  . PHP_EOL;
    }   
    echo PHP_EOL;                                                                                                                                                     
}

输出

Name: categoryId
Value: 4
Name: parentId
Value: 0
Name: description
Value: News
Name: categoryDescription
Value: 
Name: categoryName
Value: News
Name: htmlUrl
Value: http://lubranochiropractic.com/category/news/
Name: rssUrl
Value: http://lubranochiropractic.com/category/news/feed/

Name: categoryId
Value: 5
Name: parentId
Value: 4
Name: description
Value: Testimonials
Name: categoryDescription
Value: 
Name: categoryName
Value: Testimonials
Name: htmlUrl
Value: http://lubranochiropractic.com/category/news/testimonials/
Name: rssUrl
Value: http://lubranochiropractic.com/category/news/testimonials/feed/

Name: categoryId
Value: 1
Name: parentId
Value: 0
Name: description
Value: Uncategorized
Name: categoryDescription
Value: 
Name: categoryName
Value: Uncategorized
Name: htmlUrl
Value: http://lubranochiropractic.com/category/uncategorized/
Name: rssUrl
Value: http://lubranochiropractic.com/category/uncategorized/feed/

虽然它可以工作,但解析应该更复杂,因为它们使用 xml 来指定类型。

于 2012-12-11T20:22:55.230 回答