public
Last active

This code is a fix which retries up to ten times to retrieve Google Calendar in the gcalendar-wrapper.php script.

  • Download Gist
gcalendar-wrapper fix
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
/**
* Retrieve calendar embedding code
*/
 
$calRaw = '';
if (in_array('curl', get_loaded_extensions())) {
//counter for retry attempts
$i = 1;
//Let's act like a request has been made and returned error 302 so we process first attempt
$status['http_code'] = '302';
// Retry as long as 302 error returned but limit it to 10 total tries
while (($status['http_code'] == '302') AND ($i <= 10)) {
$curlObj = curl_init();
curl_setopt($curlObj, CURLOPT_URL, $calUrl);
curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, 1);
if (function_exists('curl_version')) {
$curlVer = curl_version();
if (is_array($curlVer) && !empty($curlVer['version']) &&
version_compare($curlVer['version'], '7.15.2', '>=') &&
!ini_get('open_basedir') && !ini_get('safe_mode')) {
// enable HTTP redirect following for cURL:
// - CURLOPT_FOLLOWLOCATION is disabled when PHP is in safe mode
// - cURL versions before 7.15.2 had a bug that lumped
// redirected page content with HTTP headers
// http://simplepie.org/support/viewtopic.php?id=1004
curl_setopt($curlObj, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($curlObj, CURLOPT_MAXREDIRS, 5);
}
//Get http_code from response
$calRaw = curl_exec($curlObj);
$status = curl_getinfo($curlObj);
curl_close($curlObj);
}
}
 
} else if (ini_get('allow_url_fopen')) {
// fopen should follow HTTP redirects in recent versions
//counter for retry attempts
$i = 1;
//Let's act like a request has been made and returned error 302 so we process first attempt
$meta[0] = 'HTTP/1.0 302 Moved Temporarily';
// Retry as long as 302 error returned but limit it to 10 total tries
while (($meta[0] == 'HTTP/1.0 302 Moved Temporarily') AND ($i <= 10)) {
$fp = fopen($calUrl, 'r');
while (!feof($fp)) {
$calRaw .= fread($fp, 8192);
}
//Get response
$meta = stream_get_meta_data($fp);
fclose($fp);
}
} else {
trigger_error("Can't use cURL or fopen to retrieve Google Calendar", E_USER_ERROR);
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.