darklexus2k9 Posted April 6, 2017 Share Posted April 6, 2017 ok i am trying to read an remote xml file from my isp that has some live channels from my tv service i am using php on a local server todo this and output it as an m3u for all my devicesi have an issue tho in browser the xml file outputs fine and looks good but the moment i try to load it into simplexml it failes every single time the issue i see is that in the error logs the xml seems to show as if its been html encoded<name> becomes <name>i have tried running html decoding on the string before passing it to simple xml still the same problem tho and i have tried decoding as its passed to xml with $API_CHANNELS = new SimpleXMLElement(html_entity_decode($data)); but again same issue i cant seem to figure out a way to fix it has anyone else ever had this issue Quote Link to comment https://forums.phpfreaks.com/topic/303642-simplexmlelement-error-loading-string/ Share on other sites More sharing options...
requinix Posted April 6, 2017 Share Posted April 6, 2017 If the feed is XML then you do not do anything to it before loading it into SimpleXML. $API_CHANNELS = new SimpleXMLElement($data);What error are you getting and what is the rest of the code involved? Quote Link to comment https://forums.phpfreaks.com/topic/303642-simplexmlelement-error-loading-string/#findComment-1545108 Share on other sites More sharing options...
darklexus2k9 Posted April 6, 2017 Author Share Posted April 6, 2017 (edited) the incoming xml contains a lot of special chars and is not true formatter xml and is also sent as plain text from a remote server viewing the page source shows an xml structurei have tried doing it with out any encoding or decoding but still the same issuethis is the erro i get SimpleXMLElement::__construct(): <name>5 USA (Now: 22:00 Law & Order: Special Victims Unit)</name> in /home/******/public_html/m3u.php on line 20 my codeto load the remote file is as follows // API LOGIN TO GET TOKEN $API_TOKEN = get_url("http://".$API_SERVER."/login.php?username=".$API_USERNAME."&password=".$API_PASSWORD); $data = get_url('http://'.$API_SERVER.'/livetv/channels.php?&token='.$API_TOKEN); $API_CHANNELS = new SimpleXMLElement($data); foreach ($API_CHANNELS as $API_CHANNEL){ $image=$API_CHANNEL->logo; $sd=$API_CHANNEL->url; $hd=$API_CHANNEL->url_hd; $plot=$API_CHANNEL->description; } function get_url($url) { //user agent is very necessary, otherwise some websites like google.com wont give zipped content $opts = array( 'http'=>array( 'method'=>"GET", 'header'=>"Accept-Language: en-US,en;q=0.8rn" . "Accept-Encoding: gzip,deflate,sdchrn" . "Accept-Charset:UTF-8,*;q=0.5rn" . "User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:19.0) Gecko/20100101 Firefox/19.0 FirePHP/0.4rn" ) ); $context = stream_context_create($opts); $content = file_get_contents($url ,false,$context); //If http response header mentions that content is gzipped, then uncompress it foreach($http_response_header as $c => $h) { if(stristr($h, 'content-encoding') and stristr($h, 'gzip')) { //Now lets uncompress the compressed data $content = gzinflate( substr($content,10,- ); } } return $content; } as you can see i am loading the remote page through a function to grab it properly as it is gzip encoded if you echo out $data all shows fine but the moment simple xml tries to load it < and > all turn to there respective entities Edited April 6, 2017 by darklexus2k9 Quote Link to comment https://forums.phpfreaks.com/topic/303642-simplexmlelement-error-loading-string/#findComment-1545110 Share on other sites More sharing options...
requinix Posted April 7, 2017 Share Posted April 7, 2017 What is the exact contents of $data? $data = get_url('http://'.$API_SERVER.'/livetv/channels.php?&token='.$API_TOKEN); header("Content-Type: text/plain"); echo $data; exit; Quote Link to comment https://forums.phpfreaks.com/topic/303642-simplexmlelement-error-loading-string/#findComment-1545138 Share on other sites More sharing options...
darklexus2k9 Posted April 7, 2017 Author Share Posted April 7, 2017 content is as follows <?xml version="1.0" encoding="UTF-8"?> <channels> <channel> <name>ITV (Now: No Show Namw Found! )</name> <logo>http://www.itv.com/entity/32aa2a6e-046e-4ccd-bb89-9147c581dc43/image.jpg.ashx?q=80&format=jpg&w=1366&h=769</logo> <description>No Show Description!</description> <url_sd>https://itv1liveios-i.akamaihd.net/hls/live/203437/itvlive/ITV1MN/master.m3u8</url_sd> <url_hd>https://itv1liveios-i.akamaihd.net/hls/live/203437/itvlive/ITV1MN/master.m3u8</url_hd> </channel> </channels> there are about 150 diffrent <channel> child eliments but i just left one as an example also it sshould be noted the <url_hd> eliment is not there for all channels just a select few Quote Link to comment https://forums.phpfreaks.com/topic/303642-simplexmlelement-error-loading-string/#findComment-1545150 Share on other sites More sharing options...
requinix Posted April 7, 2017 Share Posted April 7, 2017 So it's basically RSS then... Â One small change to make to what you posted: foreach ($API_CHANNELS->channel /* each */ as $API_CHANNEL){But that probably won't do anything about the error you said you were getting. What is the XML when you are getting that error? Quote Link to comment https://forums.phpfreaks.com/topic/303642-simplexmlelement-error-loading-string/#findComment-1545153 Share on other sites More sharing options...
darklexus2k9 Posted April 7, 2017 Author Share Posted April 7, 2017 yeh its confusing the hell out of me because if i print data it all shows fine but then simple xml errors out because all <> have been change to there respective html eliments i dont know what todo other than regex it but i would rather not if possible due to the changing nature of the output Quote Link to comment https://forums.phpfreaks.com/topic/303642-simplexmlelement-error-loading-string/#findComment-1545154 Share on other sites More sharing options...
requinix Posted April 7, 2017 Share Posted April 7, 2017 HTML is probably interpreting the XML. Do a View Source of the page and see what the error message actually says. Quote Link to comment https://forums.phpfreaks.com/topic/303642-simplexmlelement-error-loading-string/#findComment-1545156 Share on other sites More sharing options...
darklexus2k9 Posted April 7, 2017 Author Share Posted April 7, 2017 ok i have done some editing on my code to remove some un needed junk such as the stupid ( now : show info ) form the channel names and also removed the <description> show decription here</description> this now shows as <description>null</description> i have now eliminated every un know chars other thana-z 1-0 ://=_witch are used in channel names logos urls but i dont think they should create any issues with the xmlwhen i do a quick test by dumping the requested data after these edit it all looks good in the browser and all looks good in the view-source: web broswer page as wellnow i notice the issue seems to happen as soon as i try load this data into simple xml as a string this is when my error file starts spitting out stuff like [07-Apr-2017 22:31:32 UTC] PHP Warning: simplexml_load_string(): Entity: line 1567: parser error : xmlParseEntityRef: no name in /home/****/public_html/m3u.php on line 24 [07-Apr-2017 22:31:32 UTC] PHP Warning: simplexml_load_string(): <name>Sky Action & Adventure</name> in /home/****/public_html/m3u.php on line 24 the thing to notice is that the error is showing it as <name>Sky Action & Adventure</name> in stead of showing it as it looks in the actual browser dump of <name>Sky Action & Adventure</name> so this is totaly annoying me nowhere is all of my code with the security stuff removed lol <?php error_reporting(E_ALL); //COUNT FOR CHANNEL NUMBERS $API_COUNT = '0'; $m3u = ""; // GET FUNCTIONS TO GRAB USERNAME / PASSWORD / SERVER $API_USERNAME = $_GET['u']; $API_PASSWORD = $_GET['p']; $API_SERVER = $_GET['s']; // API LOGIN TO GET TOKEN $API_TOKEN = get_url("http://".$API_SERVER."./login.php?username=".$API_USERNAME."&password=".$API_PASSWORD); $data = get_url('http://'.$API_SERVER.'./channels.php?token='.$API_TOKEN); $one = preg_replace("/<name>(.*?) (\((.*?)\))<\/name>/s", "<name>$1</name>", $data); $two = preg_replace("/<plot>(.*?)<\/plot>/s", "<plot>Null</plot>", $one); $API_CHANNELS = simplexml_load_string($two); foreach ($API_CHANNELS as $API_CHANNEL){ $image=$API_CHANNEL->logo; $sd=$API_CHANNEL->url_sd; $hd=$API_CHANNEL->url_hd; $plot=$API_CHANNEL->description; if (strpos($API_CHANNEL->name, '----') !== false) { }else{ $title = $API_CHANNEL->name } $API_COUNT = $API_COUNT + 1; if($hd == ""){ $m3u .= '#EXTINF:-'.$API_COUNT.' tvg-logo="'.$image.'",'.$title." [SD]\r\n"; $m3u .= $API_SERVER."/play.php?id=".$sd."\r\n"; }else{ $m3u .= '#EXTINF:-'.$API_COUNT.' tvg-logo="'.$image.'",'.$title." [SD]\r\n"; $m3u .= $API_SERVER."/play.php?id=".$sd."\r\n"; $m3u .= '#EXTINF:-'.$API_COUNT.' tvg-logo="'.$image.'",'.$title." [HD]\r\n"; $m3u .= $API_SERVER."/play.php?id=".$hd."\r\n"; } } function get_url($url){ //user agent is very necessary, otherwise some websites like google.com wont give zipped content $opts = array( 'http'=>array( 'method'=>"GET", 'header'=>"Accept-Language: en-US,en;q=0.8rn" . "Accept-Encoding: gzip,deflate" . "Accept-Charset:UTF-8,*;q=0.5rn" . "User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:19.0) Gecko/20100101 Firefox/19.0 FirePHP/0.4rn" ) ); $context = stream_context_create($opts); $content = file_get_contents($url ,false,$context); //If http response header mentions that content is gzipped, then uncompress it foreach($http_response_header as $c => $h) { if(stristr($h, 'content-encoding') and stristr($h, 'gzip')) { //Now lets uncompress the compressed data $content = gzinflate( substr($content,10,- ); } } return $content; } header("content-type: audio/x-mpegurl"); header("Content-Disposition: attachment; filename=Playlist.m3u"); ?> #EXTM3U<?php echo " \r\n"?> <?php echo $m3u; ?> Quote Link to comment https://forums.phpfreaks.com/topic/303642-simplexmlelement-error-loading-string/#findComment-1545161 Share on other sites More sharing options...
Solution requinix Posted April 8, 2017 Solution Share Posted April 8, 2017 Stop touching the raw XML. Leave it alone. If you want to fix the name and... whatever you're doing to the plot... then do it after the XML has been loaded and do it to those variables you're using. $data = get_url('http://'.$API_SERVER.'./channels.php?token='.$API_TOKEN); header("Content-Type: text/plain"); echo $data; exit; $API_CHANNELS = simplexml_load_string($data); $title = (string)$API_CHANNEL->name; $title = preg_replace('#(.*?) \(.*?\)#s', '$1', $title);If you do what I said there then you won't see M3U stuff and you will see XML. I'll ask again: what XML do you see? Quote Link to comment https://forums.phpfreaks.com/topic/303642-simplexmlelement-error-loading-string/#findComment-1545164 Share on other sites More sharing options...
darklexus2k9 Posted April 8, 2017 Author Share Posted April 8, 2017 i fixed the issue god knows what is wrong but simplexml refused to read the code from the remote source it was all being tml encoded when loaded by simplexml for some reason meaning it couldnt find the child eliments i have decided to change the code a little and i am now loading it by setting up a new dom and doin it that way and all seem to be good with that so am guessing there must be issues with server files or something for the simplexml god knows but all working good nowthanks for all the help guys Quote Link to comment https://forums.phpfreaks.com/topic/303642-simplexmlelement-error-loading-string/#findComment-1545192 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.