dongwuwu6104 2010-10-05 19:16
浏览 27
已采纳

Php和XML feed。 从哪儿开始? 需要一些好主意的想法

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>
  • 写回答

3条回答 默认 最新

  • drfif48428 2010-10-05 20:03
    关注

    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')
            )
        ));
    };
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码