php页面中的Echo Restful API

时间:2013-01-06 01:26:36

标签: php api curl epg

我需要绝望的帮助才能获得特定频道播放的当前电视节目。我想使用此API http://www.webtelevideo.com/api/来获取程序名称。我发现这个页面抓住了所有意大利电视频道的所有节目,但我只希望通过指定频道播放当前播出的节目。因此,没有国家/地区数据,语言,评级等...只有在查看网页时打开的电视节目的名称。我试图转到http://www.webtelevideo.com/api/getSchedule.php?channel_id=350(例如),但我想只显示CURRENT程序,只有一个字符串。我知道在网上和stackoverflow这里有很多关于API和cURL的帖子,但我在这种东西中是一个菜鸟,我真的需要你的帮助。非常感谢提前

<?php $guide = get_post_meta($post->ID, '_wpb_in_onda', true); ?>

<?php
function GetChanels()
{
    return json_decode(file_get_contents("http://www.webtelevideo.com/api/getSchedule.php"),true);
}
function GetProgramsOnChanel($ChanName)
{
    foreach(GetChanels() as $Chanel)
    {
        {
            return $Chanel["Program"];
        }
    }
    return Array("name"=>"none");
}
function GetCurrentProgramOnChanel($ChanName)
{
    foreach(GetProgramsOnChanel($ChanName) as $Program)
    {
        $Title = $Program["title"];
        $start = strtotime($Program["ProgramSchedule"]["start"]);
        $end = strtotime($Program["ProgramSchedule"]["stop"]);
        $Current = ($start < strtotime("now +1 hour") and strtotime("now +1 hour") < $end); 
        if($Current)
        {
            return $Program;
        }
    }
    return Array("title"=>"none");
}

foreach(GetChanels() as $Chan)
$CurrentProgram = GetCurrentProgramOnChanel(" . $guide . ");
echo "Ora è in onda: " . $CurrentProgram["title"] . ". ";
echo "Termina alle: " . $CurrentProgram["ProgramSchedule"]["stop"] . " (ora italiana) <br>";
?>

1 个答案:

答案 0 :(得分:2)

以下php为您提供来自api的数据。 你可以改变它,以满足你的需要=)

<?php
    function GetChanels()
    {
        return json_decode(file_get_contents("http://www.webtelevideo.com/api/getSchedule.php"),true);
    }
    function GetProgramsOnChanel($ChanName)
    {
        foreach(GetChanels() as $Chanel)
        {
            if($Chanel["name"] == $ChanName)
            {
                return $Chanel["Program"];
            }
        }
        return Array("name"=>"none");
    }
    function GetCurrentProgramOnChanel($ChanName)
    {
        foreach(GetProgramsOnChanel($ChanName) as $Program)
        {
            $Title = $Program["title"];
            $start = strtotime($Program["ProgramSchedule"]["start"]);
            $end = strtotime($Program["ProgramSchedule"]["stop"]);
            $Current = ($start < strtotime("now +1 hour") and strtotime("now +1 hour") < $end); // + 1 hour is for timezone adjustment
            if($Current)
            {
                return $Program;
            }
        }
        return Array("title"=>"none");
    }

    function GetNextProgramOnChanel($ChanName)
    {
        $HitCurrent = false;
        foreach(GetProgramsOnChanel($ChanName) as $Program)
        {
            if($HitCurrent)
            {
                return $Program;
            }
            $Title = $Program["title"];
            $start = strtotime($Program["ProgramSchedule"]["start"]);
            $end = strtotime($Program["ProgramSchedule"]["stop"]);
            $Current = ($start < strtotime("now +1 hour") and strtotime("now +1 hour") < $end); // + 1 hour is for timezone adjustment
            if($Current)
            {
                $HitCurrent = true;
            }
        }
        return Array("title"=>"none");
    }
    /*
    foreach(GetChanels() as $Chan)
    {
        $Program = GetCurrentProgramOnChanel($Chan["name"]);
        echo "The current program in:  " . $Chan["name"] . "  is : " . $Program["title"] . "<br>";
    }
    */

    $guide = get_post_meta($post->ID, '_wpb_in_onda', true);
    $CurrentProgram = GetCurrentProgramOnChanel($guide);
    echo "Ora è in onda: " . $CurrentProgram["title"] . ". ";
    echo "Termina alle: " . $CurrentProgram["ProgramSchedule"]["stop"] . " (ora italiana) <br>";
?>