Jump to content

PHP Stream URL from XML Playlist


samuel90

Recommended Posts

Hello everyone,

 

first of all I want to apologise if I posed in wrong place. Now to my concern. I'm not a programmer but have some basic know how from school time when I learned HTML, CSS, JavaScript and so on. I have a problem and after 1 week google research I have come to somewhere but not to a solution. Therefore I hope I'll get some Help here.
My IPTV Provider hands me the Stream URLs by XML which looks like this:
<?xml version="1.0" encoding="UTF-8"?>
  <channels>
  <channel>
  <title>TV Channel 1</title>
  <img>http:/stream.url/channel-logo.png</img>
  <info></info>
  <url>http:/stream.url/channel.m3u8</url>
  </channel>
  <channel>
 <title>TV Channel 2</title>
 <img>http:/stream.url/channel2-logo.png</img>
 <info></info>
 <url>http:/stream.url/channel2.m3u8</url>
 </channel>
 ...
 </channels>
 
Now I have created my own Playlist .m3u8 with my favorite channels, since there are a lot of them and I don't want to have all of them. My Provider changes sometimes the stream url so I have allways to replace the URL manualy. Now I thought I could have a PHP stream link like: http://mywebhost.com/myphp?channel=id.
My PHP code for now looks like this: (after 1 week of research, finally the Code gets the content from XML and shows Channel-name and URL)
<?php
  $html = "";
  
  $mytv = "http://xml.provider.com/all.php";
  
  $xml=simplexml_load_file($mytv);
  
  for($i = 0; $i < 200; $i++){
  	$title = $xml->channel[$i]->title;
 	$link = $xml->channel[$i]->url;
 	$html .= "<p>$title</p><p>$link</p>";
 	}
 echo $html;
 ?> 
Now I want to give each Channel and ID wich can be a 2 Digit number, so that my stream URL would be like this  http://mywebhost.com/myphp?channel=id or similar or even better way.
Here ist your help needed, how can I give ID number to all channels, and get the dedicated stream URL as a return for my VLC player in PC and PerfectPlayer in Android device?
 

 

Link to comment
Share on other sites

But you don't have an ID to work with - just "TV Channel N", and whatever the URL is but you said that changes.

 

If there aren't too many channels in that list then you could do something like hash the name:

echo "myphp?channel=" . sprintf("%u", crc32("TV Channel 1")); // myphp?channel=3485320925
then search the XML for the channel with a matching (by doing the same sprintf+crc32 on each one and comparing the numbers).
Link to comment
Share on other sites

@requinix
 
thank you very much for the code, I tried it and it gives numbers, but I dont know if the number given for Channel 1 will always be the same and wont change, I also did not have success with the player.

​There are like 15 Channels, I can do this for all its not a problem for me, but somehow I don't understand how the code works, and what it does?!

​@Jacques1
Using channel name would be probably better, but since there space between, I thought it wont work. I don't mind if the stream URL is not pretty, if it works.

​So, how should the code be when the URL in my playlist would look like: http://mywebhost.com/my.php?get=channel-N
Link to comment
Share on other sites

I don't mind if the stream URL is not pretty, if it works.

Oh, well then, use the title.

 

The URL would look like my.php?channel=TV%20Channel%201 or ?channel=TV+Channel+1 (whichever you would rather have) by using http_build_query; use the PHP_QUERY_RFC3986 enc_type if you want %20s instead of the default plusses.

Link to comment
Share on other sites

?channel=TV+Channel+1

 

this one seems to be less confusing. But Im really confused with the Script you suggested http_build_query. As far as I understand the XML list, every channel child has an TITLE and LOCATION tab, so whenever my created stream URL for e.g.:  /my.php?channel=TV+Channel+08 is launched, it runs the PHP script, which loads the XML, finds the channel child with TITLE containing TV Channel 08, picks up the URL from LOCATION tab and responds to Player for playback

 

​I really appreciate your help, but as a noob I'm really confused what to add to my script and where : 

Link to comment
Share on other sites

Grinding through the entire XML file whenever you want to watch a channel doesn't make much sense. Surely they don't change the URLs every few minutes.

 

A more realistic approach is to periodically scan the XML file and keep the channel URLs in a local database (MySQL, SQLite, ...). Then the channel script can simply look up the current URL and perform a redirect.

 

This is a few lines of code and two SQL queries, which should be doable for anybody with basic technical competence.

Link to comment
Share on other sites

Surely they don't change the URLs every few minutes.

 

A more realistic approach is to periodically scan the XML file and keep the channel URLs in a local database (MySQL, SQLite, ...).

 

That is true, the URLs don't change every minute, but I don't know when they change, so periodical scan would bring some issues with if for e.g. the period is every 2h do a scan and update Links if there is change, but what if an URL changes right after the scan, so that channel would be offline for the next 2 hours, right?

 

​The playlist is loaded every time I start the PerfectPlayer in my Android device, so maybe its possible to do it like every time the playlist loads, it runs the script, updates the channels in the database. And in the playlist the channels URL are the one stored in the database?!

 

 

(MySQL, SQLite, ...). Then the channel script can simply look up the current URL and perform a redirect.

 

This is a few lines of code and two SQL queries, which should be doable for anybody with basic technical competence.

 

I've heard of MySQL, but never worked with it, so for this I would have to look for some tutorials though.

 

​I really like your idea of storing the URLs somewhere and let them be updated by the script, but to be honest I have no idea how to approach this. Maybe I should look locally for someone who could help me coding.

Edited by samuel90
Link to comment
Share on other sites

You can set the period to anything you want: 30 minutes, 15 minutes, 5 minutes. If the server provides a Last-Modified or ETag header, you can have a very short period, because the script won't be doing anything as long as the XML file hasn't been updated.

 

Alternatively, the channel script could probe the URL: If the resource is down, the database is updated. However, this may not be reliable, because servers don't always send a hard 404 code for invalid URLs.

 

Personally, I'd go with periodical scans at a reasonably high frequency so that the chance of you accessing the URL just between a change and the next update is insignificant.

Link to comment
Share on other sites

If the server provides a Last-Modified or ETag header, you can have a very short period, because the script won't be doing anything as long as the XML file hasn't been updated.

 

A period of 30minutes could do the job, or the one with Last-Modified​ or ETag  sounds great too, but I have no idea if my free server has something like that, how can I find out? I'll try google first, and see what I get.

 

​This Last-Modified thing is for SQL right?

 

​when I visit my server from IE i get this:

 

 

post-203519-0-72712700-1487098505_thumb.jpg

Edited by samuel90
Link to comment
Share on other sites

No, no, no. Those are HTTP headers which may or may not be present in the HTTP responses of your IPTV provider. They tell you whether the XML file has been changed. If it hasn't, the script can stop immediately and doesn't have to touch the channels at all.

 

Open the developer tools of your browser to inspect the response headers. When in doubt, use Google. This is all well-documented standard web stuff.

  • Like 1
Link to comment
Share on other sites

Just before I was about to give up, I have found something maybe usefull.

So, I found a script that loads the XML and inserts channel_name and channel_link to my database

​The script looks like this:

<?php

$con = mysql_connect("localhost","user","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("user", $con);

$mytv = "http://http://xml.provider.com/all.php";

if(!$xml=simplexml_load_file($mytv)){
trigger_error('Error reading XML file',E_USER_ERROR);
}

foreach ($xml as $channel) {
$title = $channel->title;  
$link = $channel->url;     

$sql="INSERT INTO mytvdb (channel_name, channel_url) VALUES ('$title','$link')";
$query = mysql_query($sql);

if (!$query)
    {
        echo ('Error: ' . mysql_error());
    }
    else 
    {
        echo "Record added";
    }
}
mysql_close($con)

?>   

I did the script in a new file for test purpose, so it should remain a separate file or?

​Every time I run the script it adds the data to the database, so I get the data double, triple and so on, so is it right way?

 

 

Link to comment
Share on other sites

The mysql_* functions are stonedead, and the last PHP version which still supports them is currently being phased out. There are big warnings in the PHP manual.

 

Use PDO, make the channel title the primary key of the table and do an update whenever a title already exists

$channelStmt = $databaseConnection->prepare('
    INSERT INTO
        channels (title, url)
    VALUES
        (:title, :url)
    ON DUPLICATE KEY UPDATE
        url = VALUES(url)
');
$channelStmt->execute([
    'title' => $channel->title,
    'url' => $channel->url,
]);
Link to comment
Share on other sites

I really apreciate your Help, but Im not able to see an end of this puzzle since I would have probably to learn all about Programming till I get what I want :(

​I thought It would be a simple short script that just does the job for me, but now Im spending too much trying to find some appropriate script which I then put together by try & fail method.

​Thanks again for your help

Link to comment
Share on other sites

This is the script. All you have to do now is establish a database connection (the code is on the page I've pointed you to) and insert your XML stuff. That's 5 minutes of work at most.

 

If this is too much for you, I wonder what you expected. A magical load_iptv_channel() function?

 

There is no big “puzzle” to be solved. This is about common sense and a bit of Google fu.

Link to comment
Share on other sites

Trying to connect with PDO I only get errors

<?php
$host = "localhost";
$user = "1301168";
$pass = "*****";
$db   = "1301168";

try {
    $conn = new PDO("mysql:host=$host;dbname=$db", $user, $pass);
    echo "Connected to $db at $host successfully.";
} catch (PDOException $pe) {
    die("Could not connect to the database $db :" . $pe->getMessage());
}
?>   

Error: Parse error: syntax error, unexpected '$conn' (T_VARIABLE) in /home/vhosts/mili.freeoda.com/test4.php on line 9
 

I have nothing else in the Script, and it does not work :confused:  :confused:  :confused:  :  : 
 

Link to comment
Share on other sites

Trying to connect with PDO I only get errors

 

No offense, but you have a strange tendency to always run into the opposite direction when you've just been told the right way.

 

I just gave you a link to a PDO-for-dummies tutorial. All you have to do is copy and paste the code.

<?php

$host = '127.0.0.1';
$db   = 'test';
$user = 'root';
$pass = '';
$charset = 'utf8';

$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$opt = [
    PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    PDO::ATTR_EMULATE_PREPARES   => false,
];
$databaseConnection = new PDO($dsn, $user, $pass, $opt);

Seems like PDO may not be installed/configured?

 

Sounds more like a syntax error, maybe in a different script. The line numbers don't add up.

Link to comment
Share on other sites

No offense, but you have a strange tendency to always run into the opposite direction when you've just been told the right way.

 

I just gave you a link to a PDO-for-dummies tutorial. All you have to do is copy and paste the code.

<?php

$host = '127.0.0.1';
$db   = 'test';
$user = 'root';
$pass = '';
$charset = 'utf8';

$dsn = "mysql:host=$host;dbname=$db;charset=$charset";
$opt = [
    PDO::ATTR_ERRMODE            => PDO::ERRMODE_EXCEPTION,
    PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC,
    PDO::ATTR_EMULATE_PREPARES   => false,
];
$databaseConnection = new PDO($dsn, $user, $pass, $opt);

Sounds more like a syntax error, maybe in a different script. The line numbers don't add up.

​For you it might be very easy this thing, but for me it is really complicated. I was so fu**ing happy when I made it load the xml content to the database, and then I should better use PDO, which seems not to work for me. All I do is COPY and PASTE and of course try to understand what's written and replace some values with my values. I look like 5-6 different pages and try their method but my success rate ist too low.

And the other problem ist, I don't really know for what to google. I googled like one week till I found the first script I posted, and now passes another week google-ing and results are not very pleasant, as always very different from each other. 

 

​I'm really thankful to you trying to help me, and I would totally understand if you don't want to loose more time to reply to my issues here

Link to comment
Share on other sites

Phpmyadmin is a set of php scripts that allow you to work with mysql. It's not really what we're after, although I did see some things that answer a few questions for me. With that said, make a simple php script with this in it and access it:

 

phpinfo();

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.