Jump to content

Help catching exception.


Xdega

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

I know there seems to be very little interest in assisting here here (which sucks)  :shrug:

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
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.