tags:

views:

46

answers:

3

Hiya All,

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>
+1  A: 

This really depends on the format of the information. If you give a sample xml file we can be more specific.

The basics are:

  1. Load a file with SimpleXML (load_file or load_string)
  2. Loop through the node that contains the names to see if the person is online
Galen
Hi Galen. Thank you for the answer. Added a example of the XML.
Bulfen
+3  A: 

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')
        )
    ));
};
Gordon
Thank you Gordon for the example. Should work out just the way I wanned it to. :)
Bulfen
@Bulfen if the file to parse is very large, you might want to consider using XMLReader instead. DOM (and SimpleXML) read the entire XML document into memory first. XML Reader doesnt, but the approach is different. You should not have a problem finding examples for it on StackOverflow though. Also note that iterating over an XML file (like shown and suggested elsewhere) might turn out slower than using XPath if you have many nodes to iterate.
Gordon
@Gordon. The file is around 500kB so I'll try to look abit more around in the next days to speed it up alittle. Thanks to you and Luke I got a really good start on it. :D
Bulfen
@Gordon Gordon. I just wanned it to give me a Online/Offline reply but you last example is just perfect. Thank you so very much. It's perfect.
Bulfen
@Bulfen you're welcome.
Gordon
+1  A: 

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.

Luke
Hi Luke. Thank you the answer. Tried it and it works great. ;)
Bulfen
You're welcome. Although if your file is very large, you might want to consider Gordon's answer too. His example will work faster on a larger file.
Luke
@Luke. I'll be working alittle with both examples and see what I end up with in the end. ;)
Bulfen