Php SimpleXMLElement对象变量

时间:2013-07-26 11:08:32

标签: php xml arrays variables

这是我的问题,下面我有一个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";
}



?>

2 个答案:

答案 0 :(得分:0)

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

执行
<?php
$user_token = $xml->result->data->users->entries;
foreach ($user_token as $user_token1){
    echo $user_token1->user_token;
    }
?>

答案 1 :(得分: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 ($xml->result->data->users->entries as $entry) {
    echo $entry->user_token;
}