27 May, 2010

Add Events to Yahoo Calendar Using cURL

We can add events to yahoo calendar using curl or sync our custom php calendar with yahoo calendar using curl.
Yahoo calendar events have various parameters that need to be added in curl request for adding events to yahoo.
I've listed out some of the event parameters that I have known in the following code.
The code first login to yahoo with curl and after successful login it adds events to yahoo calendar.

$serviceUrl = "http://calendar.yahoo.com/";
$authUrl = "http://login.yahoo.com/config/login?";
$userAgent = "YahooSeeker-Testing/v3.9 (compatible; Mozilla 4.0; MSIE 5.5; http://search.yahoo.com/)";
$referer = "http://my.yahoo.com";
$login = "your-yahoo-username";
$password = "your-yahoo-password";
$numPostData = 22;
$cookieFileJar = "cookie.txt";
$cookie = 0;
$postData = "login=$login&passwd=$password&.src=&.tries=5&.bypass=&.partner=&.md5=&.hash=&.intl=us&.tries=1&.challenge=ydKtXwwZarNeRMeAufKa56.oJqaO&.u=dmvmk8p231bpr&.yplus=&.emailCode=&pkg=&stepid=&.ev=&hasMsgr=0&.v=0&.chkP=N&.last=&.done=" . $serviceUrl;

$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);

// Set the referrer
curl_setopt($ch, CURLOPT_REFERER, $referer);

// Set the authentication url
curl_setopt($ch, CURLOPT_URL, $authUrl);

// Set number of post fields
curl_setopt($ch, CURLOPT_POST, $numPostData);

//Set post data in key=value pair such as login=yourusername
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);

//Set filename for storing cookie information
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFileJar);

//Set ffilename for checking the stored cookie information
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieFile);

//Set option for cookie
curl_setopt($ch, CURLOPT_COOKIE, $cookie);

//set this to output the result as string and not output directly ot browser
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

//set this value to 1 if you want to redirect to the url you provided as service url
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);

//Set this option if you do not want to verify ssl
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);

//set this option if you do not want to verify peer's certificate
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);

//now execute the curl
$res = curl_exec($ch);

//check if the username and password is valid
if ((preg_match("/invalid/i", $res)) || (preg_match("/not yet taken/i", $res))) {
echo "Invalid Login";
}
else {
//logged in successfully
//TITLE => Event title
//TYPE => Event Type (10 is for Appointment by default)
//ST => Start date format is YYYYMMDDTHHMMSS
//DUR => Total duration in HHMM format
//DESC => is event description
//in_loc => is event location

//other parameters that can be set are
//in_csz => city/state/zip value
//in_st => street name, can have comma in between names

$date = "20100527T130000";
$title = urlencode("Lunch with Bhupal at KFC");
$loc = urlencode("Kathmandu");
$desc = urlencode("Is test event");
//url for the yahoo calendar event along with event parameters
$url = "http://calendar.yahoo.com/?v=60&ST=". $date . "&TITLE=" . $title . "&VIEW=d&TYPE=10&DUR=0200&DESC=".$desc."&in_loc=".$loc;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFileJar);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieFile);
curl_setopt($ch, CURLOPT_COOKIE, $cookie);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
//get the crumb from the link
preg_match("/&.crumb=(.*)\"/", $response, $mah);

$crumbs = $mah[1];
//pass crumb value along with event parameters to yahoo caledar event url
$finalUrl = "http://calendar.yahoo.com/?v=60&ST=". $date . "&TITLE=" . $title . "&VIEW=d&TYPE=10&DUR=0200&DESC=".$desc."&in_loc=".$loc."&.crumb=".$crumbs;
curl_setopt($ch, CURLOPT_URL, $finalUrl);
curl_setopt($ch, CURLOPT_COOKIEJAR, $cookieFileJar);
curl_setopt($ch, CURLOPT_COOKIEFILE, $cookieFile);
curl_setopt($ch, CURLOPT_COOKIE, $cookie);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$finalResponse = curl_exec($ch);
echo $finalResponse;
}
?>

No comments: