主要目的就是在WordPress中通过页面展示豆瓣上的一些信息,如电影、书籍、音乐等。
特色:函数直接调用,内容缓存+图片缓存,加快访问速度
方法来自于:http://loosky.net/?p=1653
//调取用户的个人资料
function get_douban_user($userID = '',$apikey = '02a390f342bcb95707eab2d9c8362c05')
{
$url = 'http://api.douban.com/people/' . $userID . '?alt=json';
if (!empty($apikey)){
$url .= '&apikey=' . $apikey;
}
$douban_user = json_decode(file_get_contents($url), true);
return $douban_user;
}
//调取用户的收藏
function get_collections($userID = '',$type = 'movie',$num = 5,$status = 'watched',$apikey = '02a390f342bcb95707eab2d9c8362c05')
{
$url = 'http://api.douban.com/people/'.$userID.'/collection?cat='.$type.'&max-results='.$num.'&status='.$status.'&alt=json';
if (!empty($apikey))
{
$url .= '&apikey='.$apikey;
}
$result = json_decode(file_get_contents($url), true);
return $result;
}
//调取用户的评论
function get_recommendations($userID = '',$num = 5,$start = 1,$apikey = '02a390f342bcb95707eab2d9c8362c05')
{
$url = 'http://api.douban.com/people/'.$userID.'/recommendations?start-index='.$start.'&max-results='.$num.'&alt=json';
if (!empty($apikey))
{
$url .= '&apikey='.$apikey;
}
$result = json_decode(file_get_contents($url), true);
return $result;
}
//将图片缓存到本地
function cachapic($picurl,$cacheday=14){
$cachetime = $cacheday*24*60*60; //默认设定14天
//获取图片的文件名
$picquery = parse_url($picurl);
$pic=explode('/',$picquery['path']);
$picname=$pic[2];
$e = TEMPLATEPATH.'/cache/'.$picname;
$a = get_bloginfo('template_url'). '/cache/'.$picname;
if ( !is_file($e) || (time() - filemtime($e)) > $cachetime ){
copy($picurl, $e);$a = esc_attr($picurl); //第一次的时候使用真实地址
}
return $a;
}
function doubanshow($douban_user_id,$with_user_info=TRUE,$recommendationnum=5,$apikey = '02a390f342bcb95707eab2d9c8362c05'){
$collection = array(
'movie' => array(
'wish' => array(
'lang' => "想看",
'num' => 10
),
'watched' => array(
'lang' => "已看",
'num' => 25
)
),
'book' => array(
'wish' => array(
'lang' => "想读",
'num' => 10
),
'reading' => array(
'lang' => "正在读",
'num' => 10
),
'read' => array(
'lang' => "已读",
'num' => 15
)
),
'music' => array(
'wish' => array(
'lang' => "想听",
'num' => 10
),
'listening' => array(
'lang' => "正在听",
'num' => 10
),
'listened' => array(
'lang' => "已听",
'num' => 15
)
)
);
if(!$html = wp_cache_get('DoubanShow', 'Loosky')) {
$html = '<div id="douban_collections" class="douban_collections_column">';
//用户信息
if($with_user_info==true) {
$douban_user =get_douban_user($douban_user_id,$apikey);
$html .= '<div id="dc_user">'
. '<div id="dc_user_pic">'
. '<a href="' . $douban_user['link'][1]['@href'] . '" title="' . $douban_user['title']['$t'] . '" target="_blank">'
. '<img class="dc_user_image" src="' . cachapic($douban_user['link'][2]['@href']). '" alt="' . $douban_user['title']['$t'] . '" />'
. '</a></div>'
. '<ul id="dc_user_info">'
. '<li class="dc_user_info_title">'
. '<a href="' . $douban_user['link'][1]['@href'] . '" title="' . $douban_user['title']['$t'] . '" target="_blank">' . $douban_user['title']['$t'] . '</a>';
if(!empty($douban_user['db:signature']['$t'])){
$html .= '<span class="dc_user_signature"> "' . $douban_user['db:signature']['$t'] . '"</span>';
}
$html .= '</li>'
. '<li class="dc_user_info_item">Id: ' . $douban_user['db:uid']['$t'] . '</li>'
. '<li class="dc_user_info_item">Home Page: <a href="' . $douban_user['link'][3]['@href']. '" title="' . $douban_user['link'][3]['@href'] . '" target="_blank">' . $douban_user['link'][3]['@href'] . '</a></li>'
. '<li class="dc_user_info_item">Location: ' . $douban_user['db:location']['$t'] . '</li>'
. '<li class="dc_user_info_item">' . $douban_user['content']['$t'] . '</li>'
. '</ul>'
. '</div>';
}
//电影,书籍,音乐等信息
foreach ($collection as $cat => $cat_arr)
{
foreach ($cat_arr as $key => $val)
{
$collections = get_collections($douban_user_id,$cat,$val['num'],$key,$apikey);
$html .= '<div class="collection"><h2>'.$val['lang'].'</h2>';
foreach ($collections['entry'] as $val)
{
$html .= '<div class="collection_list"><a href="'.$val['db:subject']['link'][1]['@href'].'" title="'.$val['db:subject']['title']['$t'].'" target="_blank"><img class="collection_list_img" src="'.cachapic($val['db:subject']['link'][2]['@href']).'" alt="'.$val['db:subject']['title']['$t'].'"/></a></div>';
}
$html .= '</div>';
}
}
//推荐信息
$result= get_recommendations($douban_user_id,$recommendationnum,$start = 1,$apikey);
$html .= '<div class="recommendations"><h2>推荐</h2><ul>';
foreach ($result['entry'] as $val)
{
$html .= '<li>'.$val['content']['$t'].'['.date("Y-m-d H:i:s",strtotime($val['published']['$t'])).']</li>';
}
$html .= '</ul></div>';
wp_cache_add('DoubanShow',$html, 'Loosky');
}
echo $html;
}
#douban_collections {
}
.douban_collections_column {
display: inline;
float: left;
margin: 0 15px;
overflow: hidden;
/* width: 900px; */
}
#douban_collections #dc_user {
margin: 25px 0 10px;
background-color: #eee;
padding: 7px 5px 5px 10px;
width: 495px;
height: 150px;
opacity: 1.0;
overflow: hidden;
-moz-border-radius: 15px;
-webkit-border-radius: 15px;
border-radius: 15px;
-moz-box-shadow: 5px 5px 10px #444;
-webkit-box-shadow: 5px 5px 10px #444;
box-shadow: 5px 5px 10px #444;
behavior: url(images/PIE.htc);
}
#douban_collections #dc_user #dc_user_pic {
float: left;
margin: 10px 5px;
width: 120px;
height: 120px;
}
#douban_collections #dc_user #dc_user_pic img.dc_user_image {
width: 120px;
height: 120px;
}
#douban_collections #dc_user #dc_user_pic img.dc_user_image:hover {
opacity: 0.7;
}
#douban_collections #dc_user ul#dc_user_info {
float: left;
margin: 0 5px 5px 10px;
width: 350px;
}
#douban_collections #dc_user ul#dc_user_info li.dc_user_info_title {
float: left;
overflow: hidden;
list-style-type: none;
width: 100%;
line-height: 130%;
font-size: 28px;
margin-bottom: 10px;
}
#douban_collections li.dc_user_info_title a, #douban_collections li.dc_user_info_title a:link, #douban_collections li.dc_user_info_title a:visited {
line-height: 125%;
color: #FF6633;
}
#douban_collections li.dc_user_info_title a:hover {
line-height: 125%;
color: #FF9933;
}
#douban_collections #dc_user ul#dc_user_info li.dc_user_info_title .dc_user_signature {
color: #555;
font-family: georgia,verdana;
font-size: 16px;
font-style: italic;
vertical-align: super;
}
#douban_collections #dc_user ul#dc_user_info li.dc_user_info_item {
float: left;
overflow: hidden;
list-style-type: none;
margin-bottom: 5px;
width: 100%;
line-height: 125%;
}
#douban_collections li.dc_user_info_item a, #douban_collections li.dc_user_info_item a:link, #douban_collections li.dc_user_info_item a:visited {
line-height: 125%;
color: #FF6633;
}
#douban_collections li.dc_user_info_item a:hover {
line-height: 125%;
color: #FF9933;
}
#douban_collections ul{
padding: 0;
margin: 0;
}
.collection, .recommendations{
display:block;
float:left;
padding-left:8px;
margin-left:5px;
width:99%;
}
.recommendations li{
list-style-type:none;
margin-left:30px;
padding-top:8px;
}
.collection_list {
display:block;
float:left;
width:120px;
height:150px;
line-height:90px;
text-align:center;
}
.collection_list_img {border:0px;}
豆瓣用户ID
<?php doubanshow($douban_user_id='dianso'); ?>