As allways I'm abit over my head but this time I'm really pushing myself. hehe. I dont have any code yet for my new little project because I'm not really sure where to start.
Here is the background info of what I need to do. What I'm trying to do is a "status indicator" for one of my (adult) wecam advertising sites. The performers who is online are listed in a XML (online.xml). Thats fair enough to pull out but here is where it stops for me. What I need to make is a simple php script (ex. status.php?id=performername) that will check if the performers name is in the xml list or not and give me a online/offline reply. All good ideas and help are very welcomed. :)
XML Example (cleaned version)
<webcams_online>
<webcam account="a6632" sex="F" star="N" nickname="18brunette" priority="11289" preview_webcam="6632_webcam.jpg" number_visitors="none">
</webcam>
<webcam account="a18205" sex="F" star="N" nickname="Attraction" priority="11155" preview_webcam="18205_webcam.jpg" number_visitors="none">
</webcam>
</webcams_online>
The easiest way would be to use DOM and XPath:
public function isOnline($performer)
{
$dom = new DOMDocument;
$dom->load('webcams.xml');
$xPath = new DOMXPath($dom);
$nodes = $xPath->query(sprintf('//webcam[@nickname="%s"]', $performer));
return (bool) $webcams->length;
}
The above uses the DOM extension to load the XML file with the webcam states. It then searches for the <webcam>
element with a nickname
attribute containing the passed $performer
name. Assuming there will be only one cam per performer nickname, this method will return FALSE
if no element matched the XPath or TRUE
if it was matched.
You didnt specify how you wanted the script to respond. Assuming you are checking the cams via Ajax, a simple status.php script could look like this:
<?php
// Clean input to script
$performer = filter_input(
INPUT_GET, 'performer', FILTER_SANITIZE_STRING,
FILTER_FLAG_ENCODE_HIGH|FILTER_FLAG_ENCODE_LOW);
// Try to find webcam with nickname of performer in online cams XML
$dom = new DOMDocument;
$dom->load('webcams.xml');
$xPath = new DOMXPath($dom);
$webcams = $xPath->query(sprintf('//webcam[@nickname="%s"]', $performer));
// send a JSON response depending on the search result
header('Cache-Control: no-cache, must-revalidate');
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header('Content-type: application/json');
// create a JSON response for the calling client
if( $webcams->length === 0 ) {
echo json_encode(array('status' => 'offline'));
} else {
$webcam = $webcams->item(0);
echo json_encode(array(
'status' => 'online',
'data' => array(
'account' => $webcam->getAttribute('account'),
'sex' => $webcam->getAttribute('sex'),
'star' => $webcam->getAttribute('star'),
'nickname' => $webcam->getAttribute('nickname'),
'priority' => $webcam->getAttribute('priority'),
'preview' => $webcam->getAttribute('preview_webcam'),
'visitors' => $webcam->getAttribute('number_visitors')
)
));
};
This really depends on the format of the information. If you give a sample xml file we can be more specific.
The basics are:
A function like this should work for you
function check_user_online($file, $username)
{
$xml = simplexml_load_file($file);
foreach($xml->webcam as $cam)
{
if ($cam['nickname'] == $username)
{
return true;
}
}
return false;
}
Just call check_user_online($filename, $performer);
and replace $filename with the path to the XML file, and $performer with the nickname of the performer. It'll return true or false depending on whether or not the performer is found.