samuel90 Posted February 12, 2017 Share Posted February 12, 2017 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? Quote Link to comment https://forums.phpfreaks.com/topic/303175-php-stream-url-from-xml-playlist/ Share on other sites More sharing options...
requinix Posted February 13, 2017 Share Posted February 13, 2017 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=3485320925then search the XML for the channel with a matching (by doing the same sprintf+crc32 on each one and comparing the numbers). Quote Link to comment https://forums.phpfreaks.com/topic/303175-php-stream-url-from-xml-playlist/#findComment-1542695 Share on other sites More sharing options...
Jacques1 Posted February 13, 2017 Share Posted February 13, 2017 What's the whole point of the numeric “channel ID”? Just use the channel name. Sure, the percent encoding in the URL won't be very pretty, but I don't think prettiness is the issue here. If you absolutely must have numeric IDs, you'll have no choice but to create a database of channels. Quote Link to comment https://forums.phpfreaks.com/topic/303175-php-stream-url-from-xml-playlist/#findComment-1542699 Share on other sites More sharing options...
samuel90 Posted February 13, 2017 Author Share Posted February 13, 2017 @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?!@Jacques1Using 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 Quote Link to comment https://forums.phpfreaks.com/topic/303175-php-stream-url-from-xml-playlist/#findComment-1542706 Share on other sites More sharing options...
requinix Posted February 13, 2017 Share Posted February 13, 2017 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. Quote Link to comment https://forums.phpfreaks.com/topic/303175-php-stream-url-from-xml-playlist/#findComment-1542711 Share on other sites More sharing options...
samuel90 Posted February 14, 2017 Author Share Posted February 14, 2017 ?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 Quote Link to comment https://forums.phpfreaks.com/topic/303175-php-stream-url-from-xml-playlist/#findComment-1542722 Share on other sites More sharing options...
Jacques1 Posted February 14, 2017 Share Posted February 14, 2017 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. Quote Link to comment https://forums.phpfreaks.com/topic/303175-php-stream-url-from-xml-playlist/#findComment-1542723 Share on other sites More sharing options...
samuel90 Posted February 14, 2017 Author Share Posted February 14, 2017 (edited) 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 February 14, 2017 by samuel90 Quote Link to comment https://forums.phpfreaks.com/topic/303175-php-stream-url-from-xml-playlist/#findComment-1542733 Share on other sites More sharing options...
Jacques1 Posted February 14, 2017 Share Posted February 14, 2017 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. Quote Link to comment https://forums.phpfreaks.com/topic/303175-php-stream-url-from-xml-playlist/#findComment-1542735 Share on other sites More sharing options...
samuel90 Posted February 14, 2017 Author Share Posted February 14, 2017 (edited) 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: Edited February 14, 2017 by samuel90 Quote Link to comment https://forums.phpfreaks.com/topic/303175-php-stream-url-from-xml-playlist/#findComment-1542746 Share on other sites More sharing options...
Jacques1 Posted February 14, 2017 Share Posted February 14, 2017 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. 1 Quote Link to comment https://forums.phpfreaks.com/topic/303175-php-stream-url-from-xml-playlist/#findComment-1542747 Share on other sites More sharing options...
samuel90 Posted February 14, 2017 Author Share Posted February 14, 2017 Ok I understand, but when I open the the developer tools in Microsoft Edge and Chrome also, Im not able to find what you mean. My providers XML is: xml.tvmak.com/all.php Quote Link to comment https://forums.phpfreaks.com/topic/303175-php-stream-url-from-xml-playlist/#findComment-1542750 Share on other sites More sharing options...
samuel90 Posted February 15, 2017 Author Share Posted February 15, 2017 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 databaseThe 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? Quote Link to comment https://forums.phpfreaks.com/topic/303175-php-stream-url-from-xml-playlist/#findComment-1542755 Share on other sites More sharing options...
Jacques1 Posted February 15, 2017 Share Posted February 15, 2017 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, ]); Quote Link to comment https://forums.phpfreaks.com/topic/303175-php-stream-url-from-xml-playlist/#findComment-1542756 Share on other sites More sharing options...
samuel90 Posted February 15, 2017 Author Share Posted February 15, 2017 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 Quote Link to comment https://forums.phpfreaks.com/topic/303175-php-stream-url-from-xml-playlist/#findComment-1542760 Share on other sites More sharing options...
Jacques1 Posted February 15, 2017 Share Posted February 15, 2017 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. Quote Link to comment https://forums.phpfreaks.com/topic/303175-php-stream-url-from-xml-playlist/#findComment-1542761 Share on other sites More sharing options...
samuel90 Posted February 15, 2017 Author Share Posted February 15, 2017 I just messed up the whole script, nothing works.as for primary key, after 2hours trying I think I made it on channel title. I feel really dumb, The PDO and your script ist just confusing when compared to the one I got working last. Quote Link to comment https://forums.phpfreaks.com/topic/303175-php-stream-url-from-xml-playlist/#findComment-1542775 Share on other sites More sharing options...
samuel90 Posted February 15, 2017 Author Share Posted February 15, 2017 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 Quote Link to comment https://forums.phpfreaks.com/topic/303175-php-stream-url-from-xml-playlist/#findComment-1542777 Share on other sites More sharing options...
gizmola Posted February 15, 2017 Share Posted February 15, 2017 What server OS is your php script running under? Seems like PDO may not be installed/configured? See http://php.net/manual/en/pdo.installation.php Quote Link to comment https://forums.phpfreaks.com/topic/303175-php-stream-url-from-xml-playlist/#findComment-1542779 Share on other sites More sharing options...
Jacques1 Posted February 15, 2017 Share Posted February 15, 2017 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. Quote Link to comment https://forums.phpfreaks.com/topic/303175-php-stream-url-from-xml-playlist/#findComment-1542780 Share on other sites More sharing options...
samuel90 Posted February 15, 2017 Author Share Posted February 15, 2017 What server OS is your php script running under? Seems like PDO may not be installed/configured? See http://php.net/manual/en/pdo.installation.php I have no idea, down there it says there is a new version, but cant find a way to update. Quote Link to comment https://forums.phpfreaks.com/topic/303175-php-stream-url-from-xml-playlist/#findComment-1542794 Share on other sites More sharing options...
samuel90 Posted February 15, 2017 Author Share Posted February 15, 2017 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 Quote Link to comment https://forums.phpfreaks.com/topic/303175-php-stream-url-from-xml-playlist/#findComment-1542801 Share on other sites More sharing options...
gizmola Posted February 15, 2017 Share Posted February 15, 2017 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(); Quote Link to comment https://forums.phpfreaks.com/topic/303175-php-stream-url-from-xml-playlist/#findComment-1542805 Share on other sites More sharing options...
samuel90 Posted February 15, 2017 Author Share Posted February 15, 2017 OMG, its a nightmare, coding is way more complicated then I thought.OK, so where to put that script? Create a new php file? And access it from IE browser? Quote Link to comment https://forums.phpfreaks.com/topic/303175-php-stream-url-from-xml-playlist/#findComment-1542806 Share on other sites More sharing options...
gizmola Posted February 15, 2017 Share Posted February 15, 2017 Yes. Just stick it in the same place you had the other script(s). I thought you were already adding/changing scripts, so I didn't think it would be a problem for you. Call it anything you like -- phpinf.php for example. Quote Link to comment https://forums.phpfreaks.com/topic/303175-php-stream-url-from-xml-playlist/#findComment-1542809 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.