dadamssg Posted June 27, 2009 Share Posted June 27, 2009 I have some code that i want executed after a form gets submitted. After the user submits the form i want them sent to my homepage. BUT...i was wondering if after i initiated the header on the form validation script( to send them home), i could put some code after the header to execute? I want to submit an event to my database via the form....send them to the homepage...and then tweet that event to my twitter account. But sometimes the connection times out resulting in an error, which i don't want the user to see. Will the rest of the script be executed after the header? heres my example <?php //form validation and entering the database section// header("location: index.php");//send home //after sending them home, execute the tweet in case there is an error the user will already be //sent home $urlb = 'http://twitter.com/statuses/update.xml'; // Alternative JSON version // $url = 'http://twitter.com/statuses/update.json'; // Set up and execute the curl process $curl_handle = curl_init(); curl_setopt($curl_handle, CURLOPT_URL, "$urlb"); curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT, 2); curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER, 1); curl_setopt($curl_handle, CURLOPT_POST, 1); curl_setopt($curl_handle, CURLOPT_POSTFIELDS, "status=$tweet"); curl_setopt($curl_handle, CURLOPT_USERPWD, "$username:$password"); $buffer = curl_exec($curl_handle); curl_close($curl_handle); does that make sense? i just dont want the user to be waiting forever on the script or get an error if the curl doesn't go right Link to comment https://forums.phpfreaks.com/topic/163850-header-question/ Share on other sites More sharing options...
cunoodle2 Posted June 27, 2009 Share Posted June 27, 2009 Since the script doesn't always work properly I would suggest doing the following... 1. When the user gets to said page have it make some form of entry in your database. 2. Set up another page (that users will never see) that makes the curl connection to twitter (and/or other sites). Have the page keep on executing until it doesn't get any errors. 3. Call the above page via a cron job that runs like once every 15 minutes or something. That means worst case there is a 15 min lag (at most) between member posting said item and twitter post. Either that or cron it up every 5 minutes. Link to comment https://forums.phpfreaks.com/topic/163850-header-question/#findComment-864526 Share on other sites More sharing options...
dadamssg Posted June 27, 2009 Author Share Posted June 27, 2009 hey, thanks. Yeah i think the cron is the route im gonna take. I think im gonna add another column to my table and set its default to 0. Then have my cron pull up all entries with the new column 0, tweet those and then after it does it successfuly, set that column to 1. But does anyone know how to write code that checks if the curl was executed successfully? or do i not have to worry about that cause the script will die if the curl fails? im gonna use the curl and then update that new column in my database. Just don't want the later to happen if curl fails. Link to comment https://forums.phpfreaks.com/topic/163850-header-question/#findComment-864729 Share on other sites More sharing options...
cunoodle2 Posted June 27, 2009 Share Posted June 27, 2009 Just do this... <?php if(curl_exec($curl_handle)) { //you have success here!! } else { //curl did not work } ?> Curl_exec returns true on success or false on failure. However, if the CURLOPT_RETURNTRANSFER option is set, it will return the result on success, FALSE on failure. For more info see here... http://us.php.net/manual/en/function.curl-exec.php Link to comment https://forums.phpfreaks.com/topic/163850-header-question/#findComment-864742 Share on other sites More sharing options...
dadamssg Posted June 27, 2009 Author Share Posted June 27, 2009 awesome thanks...ill play with this, see what happens thanks! Link to comment https://forums.phpfreaks.com/topic/163850-header-question/#findComment-864748 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.