Xdega Posted March 19, 2011 Share Posted March 19, 2011 So basically I have a function that grabs a daily bible scripture via an rss feed. For whatever reason, it is offline today and I was greeted with a huge paragraph of errors and the bottom part of my page not rendering. Here is the function: <?php function scripture(){ function fetchXML($url) { $ch = curl_init(); $useragent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1"; curl_setopt ($ch, CURLOPT_URL, $url); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch, CURLOPT_USERAGENT, $useragent); //get data $data = curl_exec($ch); curl_close($ch); return $data; } define("USE_CURL", true); $url = "http://www.mybiblescripture.com/rss/bible.php?fmt=daily&trans=KJV"; //get xml doc with info $data = fetchXML($url); //create a SimpleXML object to parse the xml $char_xml = new SimpleXmlElement($data); $scripture_a=$char_xml->channel->item->description; $scripture_b=$char_xml->channel->item->title; echo $scripture_a . '<br><b>'. $scripture_b. '</b>'; } ?> And here is the nasty error I receive due to the url being offline: Fatal error: Uncaught exception 'Exception' with message 'String could not be parsed as XML' in /path/lbcfunc.php:21 Stack trace: #0 /path/lbcfunc.php(21): SimpleXMLElement->__construct('') #1 /path/index.php(31): scripture() #2 {main} thrown in /path/lbcfunc.php on line 21 I have 0 experience in catching exceptions and unsure where to start. I would like to try and just echo: "No Bible Scripture Found" as a friendly message instead I guess. thanks. Link to comment https://forums.phpfreaks.com/topic/231098-help-catching-exception/ Share on other sites More sharing options...
Xdega Posted March 19, 2011 Author Share Posted March 19, 2011 anyone? I tried adding <?php catch (Exception $e) { echo 'Caught exception: ', $e->getMessage(), "\n"; } ?> directly underneath: <?php //get xml doc with info $data = fetchXML($url); ?> But that was just giving me an error of "unexpected T_Catch"... Link to comment https://forums.phpfreaks.com/topic/231098-help-catching-exception/#findComment-1189737 Share on other sites More sharing options...
Xdega Posted March 20, 2011 Author Share Posted March 20, 2011 Anyone have any idea on this? I'm sure it's something simple. Link to comment https://forums.phpfreaks.com/topic/231098-help-catching-exception/#findComment-1189996 Share on other sites More sharing options...
Xdega Posted March 22, 2011 Author Share Posted March 22, 2011 ok. I have been working on this, and finally got it! (Kinda). I enclosed the entire function in a "try block" like so: <?php //BIBLE SCRIPTURE MODULE function scripture(){ try { function fetchXML($url) { $ch = curl_init(); $useragent="Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1"; curl_setopt ($ch, CURLOPT_URL, $url); curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt ($ch, CURLOPT_USERAGENT, $useragent); //GET DATA $data = curl_exec($ch); curl_close($ch); return $data; } define("USE_CURL", true); $url = "http://www.mybiblescripture.com/rss/bible.php?fmt=daily&trans=KJV"; //GET XML INFO $data = fetchXML($url); //CREATE SIMPLEXML OBJECT $char_xml = new SimpleXmlElement($data); $scripture_a=$char_xml->channel->item->description; $scripture_b=$char_xml->channel->item->title; echo $scripture_a . '<br><b>'. $scripture_b. '</b>'; } //CATCH EXCEPTION catch (Exception $e) { echo 'Scripture Module Offline!: ', $e->getMessage(), "\n"; } } The problem I have now though, is that the page takes a LONG time to load, before outputting the much friendlier error message (I was probably going to make this message a "fallback verse". Does anyone know how I could speed up the process though, because the time the page is taking to load is unacceptable atm. thx Link to comment https://forums.phpfreaks.com/topic/231098-help-catching-exception/#findComment-1190616 Share on other sites More sharing options...
Xdega Posted March 22, 2011 Author Share Posted March 22, 2011 I know there seems to be very little interest in assisting here here (which sucks) But I went ahead and added a short timeout option to the cURL function that I am using with the following code: <?php curl_setopt($ch, CURLOPT_TIMEOUT ,3); This addresses the issue of the page loading super slow because it cant connect to the source url, but I feel that 3 seconds may be too short of a timeout? What do you guys think? Link to comment https://forums.phpfreaks.com/topic/231098-help-catching-exception/#findComment-1191041 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.