try catch with the file function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
highjo
Forum Contributor
Posts: 118
Joined: Tue Oct 24, 2006 1:07 pm

try catch with the file function

Post by highjo »

hi! there .i have a script that runs fie until the connection was very bad.now 've realised that the normal try and catch i've done are not working very well.so i cange it to :

Code: Select all

 
try
{
   $response = file($authurl);
   if(!$response) throw new Exception("We are facing some difficulties.please try again");   
}
catch(Exception $e)
{
   die($e->getMessage());
}
 
but i'm still having the same error
Fatal error: Maximum execution time of 60 seconds exceeded in
any idea?
Last edited by John Cartwright on Mon Jun 16, 2008 1:31 pm, edited 1 time in total.
Reason: Fixed your indentation
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: try catch with the file function

Post by John Cartwright »

Code: Select all

$response = file($authurl);
.. is the likely culprit. What are you trying to load? A large file?
User avatar
highjo
Forum Contributor
Posts: 118
Joined: Tue Oct 24, 2006 1:07 pm

Re: try catch with the file function

Post by highjo »

actually, it's a api call. i use file with get parameter in the address to have an session id before i send sensitive info with that session id to another url with a proper curl request
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Re: try catch with the file function

Post by John Cartwright »

I would recommend using cURL to make the requests, and perhaps increase set_time_limit(0) if your having latency issues.
User avatar
highjo
Forum Contributor
Posts: 118
Joined: Tue Oct 24, 2006 1:07 pm

Re: try catch with the file function

Post by highjo »

i'll try that. the file does the job in one line.Don't you know any king of mesure to catch error either for file and curl if for example the connection break? as i say the normal try and catch seems not to be working, same if i throw the exception myself.Am i doing something wrong?
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: try catch with the file function

Post by Eran »

You can only catch exceptions if they are thrown before a fatal error. Since you are calling a php function which fails with a timeout before you can throw the exception, your error handling will not work.
User avatar
highjo
Forum Contributor
Posts: 118
Joined: Tue Oct 24, 2006 1:07 pm

Re: try catch with the file function

Post by highjo »

i thinks i understand the point.Now its time for alternatives.the application is abuot to send sms on the net and its working as i told you.this part of the application that is giving me the problem is related to giving a delivery status at least 40 seconds after the message has been send with a modal pop up.so i query for the status with the message id by calling the api (this is the very one that show the ) in a loop 5 times.
PS: i have a proper cron job which would do that every 5mn.But for user experience i thought i should give a temporary delivery status after 40 seconds.
That's it.
Need to think differently for that, so i need your suggestion.Thanks for helping
Post Reply