Category Archive:

Using the google calendar api to delete an event with zend framework

0

I’m using zend framework’s GData library to work with google calendar api v2 and I’d implemented a solution to add and delete events. The adding of events works well, but deleting them did not. According to zend, the way to delete an event is like this

// Option 1: Events can be deleted directly
$event->delete();
 
// Option 2: Events can be deleted supplying the edit URL of the event
// to the calendar service, if known
$service->delete($event->getEditLink()->href);

Deleting via option #1

So after adding the event via the api, an object of Zend_Gdata_Calendar_EventEntry is returned and I should be able to simply do $event->delete(); to delete the event, but this does not work and returns the following error: “Expected response code 200, got 405. Http method DELETE is not supported by this URL”. It’s what the documentation says to do and it doesn’t work.

Deleting via option #2

So, let’s try the other way by storing the “edit link”. First we add the calendar entry successfully, then take the instance of Zend_Gdata_Calendar_EventEntry that is returned and call the delete as below

$event = $cal->addEvent(/* my own method that adds the event and returns instance of Zend_Gdata_Calendar_EventEntry */);
 
$link = $event->getEditLink()->href;
 
function deleteEventByEditLink($link) {
	$client = getClient();
	$service = new Zend_Gdata_Calendar($client);
 
	try {
		$response = $service->delete($link);
	} catch(Exception $e) {
		//event most likely didn't exist on the calendar. 
		return false;
	}
	return true;
}
 
function getClient() {
	return Zend_Gdata_ClientLogin::getHttpClient(GOOGLE_CALENDAR_USERNAME, GOOGLE_CALENDAR_PASSWORD, Zend_Gdata_Calendar::AUTH_SERVICE_NAME);
}

The $response variable above results in a NULL value and the entry does not delete.

What does work – Yes, how to actually delete events!

I was able to find a working solution by storing the event id rather than the edit link, then loading the event by id through the api and then calling the delete method as below.

/* after adding, extract and save the event id */
$event = $cal->addEvent();
$eventId = $event->id->text;
 
/* deleting */
$result = deleteEventById($eventId);
 
function deleteEventById($eventId) {
	$event = getEventById($eventId);
	try {
		$response = $event->delete();
	} catch(Exception $e) {
		//event most likely didn't exist on the calendar. 
		return false;
	}
	return true;
}
 
function getEventById($eventId) {
	// Create an authenticated HTTP client
	$client = getClient();
	$service = new Zend_Gdata_Calendar($client);
	try {
		return $service->getCalendarEventEntry($eventId);
	} catch (Zend_Gdata_App_Exception $e) {
		echo "Error: " . $e->getMessage();
	}
}
 
function getClient() {
	return Zend_Gdata_ClientLogin::getHttpClient(GOOGLE_CALENDAR_USERNAME, GOOGLE_CALENDAR_PASSWORD, Zend_Gdata_Calendar::AUTH_SERVICE_NAME);
}

Hope this saves somebody six hours of pain. Google calendar has API V3 out that seems to work pretty well, except for the oauth authentication bit… have fun with that.

Posted in: PHP

Continue Reading

WordPress filter when inserting post breaks my html

0

I’m programmatically inserting posts into wordpress and all of a sudden the drop down box I’d coded into the content was getting changed sometime between when I’d call wp_insert_post and when it gets saved in the database. The select and option tags would get stripped out and a p tag would get tossed in there randomly. The solution was to disable the filtering wordpress does on the $postarr by doing the following:

kses_remove_filters();
wp_insert_post( $postArray );
kses_init_filters();
Posted in: Wordpress

Continue Reading

php create invalid utf-8 character

0

Use the chr() function and any number above 127 to create an invalid utf8 character. This may be useful if you want to test your UTF-8 validation script. See the below code as an example. It’ll print ‘UTF-8′ if the character is valid UTF-8 or nothing otherwise.

echo mb_detect_encoding(chr(128), 'UTF-8', true);
Posted in: Character Encoding

Continue Reading

PHP detect encoding of multibyte characters

0

This function will test if the characters are UTF-8 or not. You may have to modify the character set to test against in the mb_detect_encoding function to fit your needs.

function mb_str_split( $string ) {
	return preg_split('/(?<!^)(?!$)/u', $string );
}
 
$string = "更多學習";
 
$charlist = mb_str_split($string);
echo '<table>';
foreach($charlist as $char) {
	echo '<tr>';
	echo '<td>' . $char . '</td>';
	echo '<td>' . mb_detect_encoding($char, 'UTF-8', true) . '</td>';
	echo '</tr>';
}
echo '</table>';
Posted in: Character Encoding

Continue Reading

PHP detect encoding of each character in a string

0

Here is a little script I wrote that will detect the encoding of each character in a string. If the encoding is not UTF-8, it will try to convert the character using each of the below character encodings. The result of the encoding will be printed to the screen and if the character appears as it should, then it fits that encoding. Note that the character may match more than one encoding and this will not work with multi byte characters.

$encodings = array("UTF-8", "UTF-16", "ASCII",
		"Windows-1250", "Windows-1251", "Windows-1252", "Windows-1253", "Windows-1254", "Windows-1255", "Windows-1256", "Windows-1257", "Windows-1258",
		"ISO-8859-1", "ISO-8859-2", "ISO-8859-3", "ISO-8859-4", "ISO-8859-5", "ISO-8859-6", "ISO-8859-7", "ISO-8859-8", "ISO-8859-9", "ISO-8859-10",
		"ISO-8859-11", "ISO-8859-12", "ISO-8859-13", "ISO-8859-14", "ISO-8859-15", "ISO-8859-16",
		"CP1256", "CP1250", "CP1252", 'CP437', 'CP737', 'CP850', 'CP852', 'CP855', 'CP857', 'CP858', 'CP860', 'CP861', 'CP862', 'CP863', 'CP865',
		'CP866', 'CP869', 'CP37', 'CP930', 'CP1047', 'MIK', 'ISCII', 'TSCII', 'VISCII', 'JIS X 0208', 'EUC-JP', 'GB 2312', 'GBK', 'Big5',
		'HKSCS', 'KS X 1001', 'EUC-KR', 'ISO-2022-KR', 'Mac OS Roman', 'KOI7', 'KOI8-U', 'KOI8-R', 'GB18030', 'GB2312 80'
);
 
$string = 'This is a test string';
echo '<table>';
$len = strlen($string);
for ($i = 0; $i < $len; $i++) {
	$encoding = mb_detect_encoding($string[$i], 'UTF-8', true);
	echo '<tr><td>' . $i . '</td><td>' . $string[$i] . '</td><td>' . $encoding . '</td>';
	if($encoding != 'UTF-8') {
		foreach ($encodings as $j) {
			echo '<td>' . iconv($j, 'UTF-8', $string[$i]) . '</td>';
		}
	}
	echo '</tr>';
}
echo '</table>';
Posted in: Character Encoding

Continue Reading