Zendframework 를 이용한 Google 즐겨찾기 공유

Google의 다른 서비스들은 GData라는 모델을 통해서 데이터를 가져올 수가 있는데, 구글툴바에 포함된 구글 즐겨찾기 (http://www.google.com/bookmarks/)는  개인정보라 생각해서인지, 그럴 수 없었다.

그동안 유용하다고 생각해서 추가한 즐겨찾기들을 del.icio.us처럼 공유하고 싶어서 한번 만들어 보았다.

간단히 http://www.google.com/bookmarks/lookup?output=rss&num=10000 만 읽어오면 되는데, 접근하기 위해서는 구글 인증이 필요하다.  num 이라는 parameter는 몇 개의 item을 가져올 건지 나타내는 값인데, 모두 가져오기 위해서 10000이라는 큰 값을 주었다.
<?php
Zend_Loader :: loadClass('Zend_Http_Client');
Zend_Loader :: loadClass('Zend_Feed');

$client = new Zend_Http_Client();
$client->setAuth("사용자ID", "패스워드");
$client->setHeaders('Accept-Charset','utf-8');
$client->setUri('http://www.google.com/bookmarks/lookup?output=rss&num=10000');
$response = $client->request();
if ($response->getStatus() != 200)
{
Zend_Loader :: loadClass('Zend_Exception');
throw new Zend_Exception('An error occurred while fetching data: ' .
$response->getStatus() . ': ' . $response->getMessage());
}
$body = $response->getBody();

$feed = Zend_Feed::importString($body);

// 레이블별 정렬
$feeds = array();

foreach($feeds as $entry)
{
$label = $entry->bkmk_label();
// 레이블이 두 개 이상인 경우
if(is_array($label))
{
foreach($label as $labelItem)
{
$feeds[$labelItem->nodeValue][] = $entry;
}
}
else
{
$feeds[$label][] = $entry;
}
}

ksort($feeds);
// end of 레이블별 정렬
?>
<h1>즐겨찾기</h1>
<? foreach(array_keys($feeds) as $label) : ?>
<h2><?= $label ? $label : '분류 없음' ?></h2>
<ul>
<? foreach($feeds[$label] as $entry) : ?>
<li class="<?= $entry->bkmk() ?>">
<a href="<?= $entry->link() ?>" target="_blank"><?= $entry->title() ?></a>
<span class="added">(추가된 날짜 : <span class="date">
<?= date("Y년 m월 d일 H시 i분", strtotime($entry->pubDate())) ?></span>)</span>
<? if($entry->bkmk_annotation()) : ?>
<p class="annotation"><?= $entry->bkmk_annotation() ?></p>
<? endif; ?>
</li>
<? endforeach; ?>
</ul>
<? endforeach; ?>

실행결과는 http://fguy.com/bookmarks/ 에서와 비슷하게 볼 수 있을 것이다.

댓글

이 블로그의 인기 게시물

단위테스트를 위한 주민등록번호 생성

낡은 방식으로 PHP를 사용하고 있는 조직의 개발환경 개선 방안 구상

프로그래머가 되기까지...