Created
November 2, 2010 16:23
-
-
Save mattmckenny/659887 to your computer and use it in GitHub Desktop.
This code is a fix which retries up to ten times to retrieve Google Calendar in the gcalendar-wrapper.php script.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* 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); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment