phpapicurlepg

Echo Restful API in php page


I need desperate help getting the current tv program broadcasted by a specific channel. I want to use this API http://www.webtelevideo.com/api/ to get the program name. I found this page that grabs all the programs of all the italian TV channels but I want only the currently broadcasted program by a specified channel to show. So without country data, language, rating etc... only the name of the TV program that is on at the moment of viewing the web page. I tried to go to http://www.webtelevideo.com/api/getSchedule.php?channel_id=350 (for example) but I want to make only the CURRENT program to appear, ONLY one string. I know that on the web and here at stackoverflow there are a lot of post about APIs and cURL but I'm a noob in this kind of stuff and I really need your help. Thanks a lot in advance

<?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>";
?>

Solution

  • Following php gives you the data from the api. You can change it so it fit your needs =)

    <?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>";
    ?>