我被要求为一个组织建立一个网站,他们希望我让用户注册一个(已设置的)用户ID,该用户ID存储在本地数据库中,并从提供的外部XML API中获取其余部分由'主'组织。由于我们只使用用户CID和密码,我已经设置了$_SESSION['cid']
,但我的问题是从XML文件中获取用户信息。到目前为止,我已经尝试过(无济于事):
$cid= $_SESSION['cid'];
//Get user information
if($xml = simplexml_load_file('http://api.vateud.net/members/FRA.xml')){
$count = count($xml -> member);
foreach($xml->member as $member){
if($member->cid == $cid){
//Success
} else {
//Get ID error
die;
}
}
} else {
//XML load error
die;
}
XML文件:http://api.vateud.net/members/FRA.xml
如果您查看XML文件,我将如何获取该特定用户的相应<member>
标记内的用户信息。简单来说(伪代码):我如何获得firstname
和lastname
值cid
= $_SESSION['cid']
?
答案 0 :(得分:1)
你在第一个循环之后退出,我调整了脚本以将项目拉出到数组中。
<?php
session_start();
try {
if (empty($_SESSION['cid'])) {
throw new \Exception('Session cid not defined or empty');
}
$result = [];
if ($xml = simplexml_load_file('http://api.vateud.net/members/FRA.xml')) {
foreach ($xml->member as $member) {
if ($member->cid == $_SESSION['cid']) {
$result = (array) $member;
break;
}
}
} else {
throw new \Exception('Could not load XML');
}
// do somthing with result
print_r($result);
} catch (\Exception $e) {
// store in a variable if you wish
die($e->getMessage());
}
<强>结果:强>
Array
(
[active] => true
[cid] => 800000
[country] => FR
[firstname] => Foobar
[humanized-atc-rating] => C3
[humanized-pilot-rating] => P0
[lastname] => Baz
[pilot-rating] => 0
[rating] => 7
[reg-date] => 2001-01-01 12:00:00
[subdivision] => FRA
)