<?php
error_reporting(0);
header("Content-type: text/xml; charset=utf-8");
//$url = "http://v.pptv.com/show/5W4wrhZ87Cq5N58.html";
$query = $_SERVER[QUERY_STRING];
parse_str($query);
if ($url) {
        echo pptv($url);
}
function pptv($url) {
        $pid = preg($url, '/show\/([0-9a-zA-Z]+)\.html/', 0);
        $purl = "http://api.v.pptv.com/api/openapi/player.open.json?id=$pid&from=0&version=";
        $json = json_decode(curl($purl, 0));
        $ppid = $json->data->pptv->id;
        $title = $json->data->pptv->title;
        $ppurl = "http://client-play.pplive.cn/chplay3-0-$ppid.xml";
        $ppstr = curl($ppurl, 0);
        $xml = simplexml_load_string($ppstr);
        $rid = $xml->channel[rid];
        $sh = $xml->dt->sh;
        
        $list = "<list>\n";
        
                $iurl = "http://$sh:81/$rid";
                $list .= "<m type=\"2\" src=\"$iurl\" label=\"$title\" />\n";
        
        return $list . "</list>\n";
}
function curl($url, $bool) {
                $user_agent = $_SERVER['HTTP_USER_AGENT'];
                $referer = "http://player.pplive.cn/ikan/1.0.5.26/player4player2.swf";
                $ch = curl_init();
                $timeout = 30;
                curl_setopt($ch, CURLOPT_URL, $url);
                curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
                curl_setopt($ch, CURLOPT_REFERER, $referer);
                curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
                curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
                $data = curl_exec($ch);
                curl_close($ch);

        if ($bool) {
                return iconv('gbk', 'utf-8', $data);
        }
        return $data;
}

function preg($str, $preg, $bool) {
        if ($bool) {
                preg_match_all($preg, $str, $ar);
        } else {
                preg_match($preg, $str, $ar);
        }
        return $ar[1];
}
?>