Reputation: 66
I've been asked to make a website for an organisation and they want me to let users register with a (already set) user ID, which is stored in a local database, and grab the rest from an external XML API provided by the 'master' organization. Since we are only using the user CID and password, I've already set $_SESSION['cid']
, but my problem is getting the user information from the XML file. So far I've tried (to no avail):
$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;
}
The XML file: http://api.vateud.net/members/FRA.xml
If you look at the XML file, how would I go about getting the user information inside the respective <member>
tag for that specific user. In simple words (pseudocode): how would I get the firstname
and lastname
values WHERE cid
= $_SESSION['cid']
?
Upvotes: 1
Views: 69
Reputation: 46602
Your exiting after the first loop, I tweaked the script to pull out the item into an array.
<?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());
}
Result:
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
)
Upvotes: 1