06 July, 2010

Get Google Calendar Events using cURL

Once i needed to get all the google calendar events to my site, i.e. getting all the events that we added to google calendar to our custom calendar. After some reading, i found some helpful tips for doing this and here is the code. This is just a snippet. For logging in to gmail account please check my other post. The authentication token obtained after successful login is used in the following code.
So, what this code does is, it gets google calendar events using cURL. The code is simple. Just the retrieving of xml feed of google calendar events is done here, after this we can parse the xml and display all the events to our custom page after some formatting. Checkout the code.

$fAuth = "authentication-token-obtained-after-successful-login";
$arr_headers = array();
$arr_headers[] = "Host: www.google.com";
$arr_headers[] = "MIME-Version: 1.0";
$arr_headers[] = "Accept: text/xml";
$arr_headers[] = "Authorization: GoogleLogin auth=".$fAuth;
$arr_headers[] = "Content-type: application/atom+xml";
$arr_headers[] = "Cache-Control: no-cache";
$arr_headers[] = "Connection: Keep Alive \r\n";

$url = "http://www.google.com/calendar/feeds/emialadd%40gmail.com/private/full";
$ch = curl_init();


curl_setopt($ch, CURLOPT_REFERER, "http://google.com");
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_HTTPHEADER, $arr_headers);

curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.4b) Gecko/20040612 Mozilla Firebird/0.9");
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);

$resFin = curl_exec($ch);
echo $resFin;
?>

2 comments:

Krishna DC said...

Hi

Am using oAuth2.0 with php. may i know how we can retrieve the same calendar event feeds using oauth2.0 instead of google login which you have used here with curl. Please let me know.

Thanks
Krishna

Krishna DC said...

Pls let me know