2

这是我的问题,下面我有一个 Api 脚本来手动输入 xml 对象 [user_token] 以根据令牌访问用户信息。因此,要输入数组中的每个属性,我使用了代码

foreach ($xml->result->data->users->entries->user_token as $user_token1) 
    echo $user_token1;

然后插入

if ($oneall_curly->get (SITE_DOMAIN . "/users/" . $user_token1 . ".xml"))
{
$result = $oneall_curly->get_result ();
print_r (($result->body));
}

因此,在此之后我不确定出了什么问题,我在堆栈溢出和 php.net 上搜索了许多答案。我当然不是专家,实际上只是编码 php 的新手,但我肯定会感谢对这个问题的一些见解或对创建变量 $user_token1 的代码的帮助

非常感谢您的关注!

SimpleXMLElement Object
(
[request] => SimpleXMLElement Object
    (
        [date] => Fri, 26 Jul 2013 12:18:36 +0200
        [resource] => /users.xml??page=1&order_direction=desc
        [status] => SimpleXMLElement Object
            (
                [flag] => success
                [code] => 200
                [info] => Your request has been processed successfully
            )

    )

[result] => SimpleXMLElement Object
    (
        [data] => SimpleXMLElement Object
            (
                [users] => SimpleXMLElement Object
                    (
                        [pagination] => SimpleXMLElement Object
                            (
                                [current_page] => 1
                                [total_pages] => 1
                                [entries_per_page] => 500
                                [total_entries] => 2
                                [order] => SimpleXMLElement Object
                                    (
                                        [field] => date_creation
                                        [direction] => desc
                                    )

                            )

                        [count] => 2
                        [entries] => Array
                            (
                                [0] => SimpleXMLElement Object
                                    (
                                        [user_token] => 11111111-4444-bbbb-1111-111111111111
                                        [date_creation] => Wed, 24 Jul 2013 00:33:28 +0200
                                        [date_last_login] => Wed, 24 Jul 2013 02:08:44 +0200
                                        [num_logins] => 2
                                    )

                                [1] => SimpleXMLElement Object
                                    (
                                        [user_token] => 22222222-5555-aaaa-2222-333333333333
                                        [date_creation] => Wed, 24 Jul 2013 00:28:56 +0200
                                        [date_last_login] => Thu, 25 Jul 2013 07:08:20 +0200
                                        [num_logins] => 4
                                    )

                            )

                    )

            )

    )

)

这是我从以前的代码中输出的 simpleXML 对象

$raw = "http://../../file.php";
$xml = simplexml_load_file($raw);

echo '<pre>';
print_r($xml);
echo '</pre>';
return $xml;


// Get an element by its id attribute


// or, loop over all of the <rate> elements
foreach ($xml->result->data->users->entries->user_token as $user_token1) 
    echo $user_token1;





if ($oneall_curly->get (SITE_DOMAIN . "/users/" . $user_token1 . ".xml"))
{
    $result = $oneall_curly->get_result ();
    print_r (($result->body));
}
//Error
else
{
$result = $oneall_curly->get_result ();
echo "Error: " . $result->http_info . "\n";
}



?>
4

2 回答 2

0

试试这种类型。entries是一个主数组。所以你可以存储一个变量然后你通过foreach

<?php
$user_token = $xml->result->data->users->entries;
foreach ($user_token as $user_token1){
    echo $user_token1->user_token;
    }
?>
于 2013-07-26T11:16:39.367 回答
0

这样$xml->result->data->users->entries

你选择这个:

[0] => SimpleXMLElement Object
(
    [user_token] => 11111111-4444-bbbb-1111-111111111111
    [date_creation] => Wed, 24 Jul 2013 00:33:28 +0200
    [date_last_login] => Wed, 24 Jul 2013 02:08:44 +0200
    [num_logins] => 2
)

[1] => SimpleXMLElement Object
(
    [user_token] => 22222222-5555-aaaa-2222-333333333333
    [date_creation] => Wed, 24 Jul 2013 00:28:56 +0200
    [date_last_login] => Thu, 25 Jul 2013 07:08:20 +0200
    [num_logins] => 4
)

现在你有一个 2 数组SimpleXMLElement Objects。现在你可以 foreach

foreach ($xml->result->data->users->entries as $entry) {
    echo $entry->user_token;
}
于 2013-07-26T12:00:12.140 回答