1

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>";
?>
Ankloop
  • 49
  • 1
  • 9
  • "foreach(GetChanels() as $Chan)" is not needed at the bottom, with it you will fetch the api text for each chanel! That is extreamly waste of time. – ZombieSpy Jan 07 '13 at 21:57

1 Answers1

2

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>";
?>
ZombieSpy
  • 1,376
  • 12
  • 23
  • With the code you provided me i got this: Warning: DOMDocument::loadHTMLFile() [domdocument.loadhtmlfile]: htmlParseEntityRef: expecting ';' in http://www.webtelevideo.com/#!/Notte, line: 537 in /home/tvindire/public_html/testel.php on line 3 ... I should replace prog2 prog with what? "Rai Uno"? And if I want to use that API, which code I have to use? Thanks for your help – Ankloop Jan 06 '13 at 14:37
  • Thank you!!!!!!! It works!!! But if I want to show current program for a single channel only (that I will change according to web page), what code should I use? – Ankloop Jan 07 '13 at 13:11
  • EDIT: Looking better at your code I've got it: http://bit.ly/XD7WIK . You gave me the most complete answer I could ever imagine :)) – Ankloop Jan 07 '13 at 13:17
  • Just one more question :)) If I want to show also the following tv program, what should I add to this code? – Ankloop Jan 07 '13 at 13:50
  • I noticed that this script requires some time to load. There is a way to speed up it? – Ankloop Jan 07 '13 at 14:30
  • @user1849781 A function to get next program is added =) (not tested tho) – ZombieSpy Jan 07 '13 at 20:48
  • I get this error with the new code: Warning: Invalid argument supplied for foreach() in /page.php on line 9 Fatal error: Cannot use string offset as an array in /page.php on line 23 Thank you :) – Ankloop Jan 08 '13 at 14:05
  • @user1849781 Fixed, i think – ZombieSpy Jan 08 '13 at 18:01
  • This error is still appering:: Fatal error: Cannot use string offset as an array in /page.php on line 23. I think that the problem is in GetCurrentProgramOnChanel($guide); .. but i don't know what's wrong.. $guide is a wordpress textarea stored in my database – Ankloop Jan 08 '13 at 20:35
  • Exactly what is on line 23? – ZombieSpy Jan 08 '13 at 20:44
  • Yes, I've updated the code. It refers to this line: $end = strtotime($Program["ProgramSchedule"]["stop"]); but I think that the problem is: GetCurrentProgramOnChanel($guide); because replacing $guide with "Rai 2" it works – Ankloop Jan 09 '13 at 20:21
  • @user1849781 what does "print_r($guide);" give? – ZombieSpy Jan 09 '13 at 20:52
  • It gives Rai 2 if $guide is Rai 2 – Ankloop Jan 10 '13 at 11:57
  • With $CurrentProgram = GetCurrentProgramOnChanel ($guide); it works!! But if $guide is blank it gives: Fatal error: Cannot use string offset as an array, how can I avoid this? I think that perhaps it is related with wordpress and $guide = get_post_meta($post->ID, '_wpb_in_onda', true); – Ankloop Jan 10 '13 at 13:09
  • @ankloop Use "if($guide != '')" ? – ZombieSpy Jan 11 '13 at 20:29