使用 PHP 获取 Tumblr 照片
编辑:想出一个解决方案。正如建议的那样,我将脚本转换为使用 JSON 而不是 XML。该脚本如下,如果需要的话应该可以在 WordPress 安装中运行
<?php
$offset = $_GET['offset'];
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,"http://blog.dgovil.com/api/read/json?num=10&start=".$offset);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
$result = curl_exec($ch);
curl_close($ch);
$result = str_replace("var tumblr_api_read = ","",$result);
$result = str_replace(';','',$result);
$result = str_replace('\u00a0','&nbsp;',$result);
$jsondata = json_decode($result,true);
$posts = $jsondata['posts'];
foreach($posts as $post){ ?>
<div class="tumblr_post post-<?php echo $post['type'] ?>">
<?php if ($post['type'] == 'regular') { ?>
<div class="post-title" id="post-<?php echo $post['id'];?>"><a href="<?php echo $post['url-with-slug']; ?>"><?php echo $post{'regular-title'}; ?></a></div>
<?php echo $post{'regular-body'}; ?>
<?php } ?>
<?php if ($post['type'] == 'quote') { ?>
<?php echo $post{'quote-text'}; ?>
<?php echo $post{'quote-source'}; ?>
<?php } ?>
<?php if ($post['type'] == 'photo') { ?>
<img src="<?php echo $post['photo-url-500'];?>">
<?php echo $post{'photo-caption'}; ?>
<?php echo $post{'photo-set'}; ?>
<a href="<?php echo $post{'photo-url'}; ?>" class="fImage">View Full Size</a>
<?php } ?>
<?php if ($post['type'] == 'link') { ?>
<p><a href="<?php echo $post{'link-url'}; ?>"><?php echo $post{'link-text'}; ?></a>
<?php echo $post{'link-description'}; ?>
<?php } ?>
<?php if ($post['type'] == 'conversation') { ?>
<?php echo $post{'conversation-text'}; ?>
<?php } ?>
<?php if ($post['type'] == 'video') { ?>
<!--<?php echo $post{'video-source'}; ?>-->
<?php echo $post{'video-player'}; ?>
<?php echo $post{'video-caption'}; ?>
<?php } ?>
<?php if ($post['type'] == 'conversation') { ?>
<?php echo $post{'audio-caption'}; ?>
<?php echo $post{'audio-player'}; ?>
<?php echo $post{'audio-plays'}; ?>
<?php } ?>
<div id="post-date">
<?php echo date("jS D M, H:i",strtotime($post['date'])); ?> <a href="<?php echo $post['url-with-slug']; ?>">Read on Tumblr</a>
</div>
</div>
<?php }?>
<div id="tumblr_down"></div>
<script type="text/javascript">
// load the api response
$.getJSON('http://tumblruptime.apigee.com/json?callback=?', function (response) {
// check if an api method is broken
if (!response.methods['/api/read'].up) {
$('#tumblr_down').text('Tumblr seems to not want to work with me right now. You can still try and view the posts on blog.dgovil.com. Service provided by http://tumblruptime.icodeforlove.com/apiinfo');
}
});
</script>
<div id="tumblr_postnav">
<a href="<?php echo '?offset='.($offset + 10) ?>" id="tumblr_next">Older posts</a>
<a href="<?php echo '?offset='.($offset - 10) ?>" id="tumblr_prev">Newer Posts</a>
</div>
谢谢!
EDIT: Figured out a solution. As was suggested, I converted the script to use the JSON instead of the XML. The script is as follows and should work inside a Wordpress install if needed
<?php
$offset = $_GET['offset'];
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,"http://blog.dgovil.com/api/read/json?num=10&start=".$offset);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
$result = curl_exec($ch);
curl_close($ch);
$result = str_replace("var tumblr_api_read = ","",$result);
$result = str_replace(';','',$result);
$result = str_replace('\u00a0',' ',$result);
$jsondata = json_decode($result,true);
$posts = $jsondata['posts'];
foreach($posts as $post){ ?>
<div class="tumblr_post post-<?php echo $post['type'] ?>">
<?php if ($post['type'] == 'regular') { ?>
<div class="post-title" id="post-<?php echo $post['id'];?>"><a href="<?php echo $post['url-with-slug']; ?>"><?php echo $post{'regular-title'}; ?></a></div>
<?php echo $post{'regular-body'}; ?>
<?php } ?>
<?php if ($post['type'] == 'quote') { ?>
<?php echo $post{'quote-text'}; ?>
<?php echo $post{'quote-source'}; ?>
<?php } ?>
<?php if ($post['type'] == 'photo') { ?>
<img src="<?php echo $post['photo-url-500'];?>">
<?php echo $post{'photo-caption'}; ?>
<?php echo $post{'photo-set'}; ?>
<a href="<?php echo $post{'photo-url'}; ?>" class="fImage">View Full Size</a>
<?php } ?>
<?php if ($post['type'] == 'link') { ?>
<p><a href="<?php echo $post{'link-url'}; ?>"><?php echo $post{'link-text'}; ?></a>
<?php echo $post{'link-description'}; ?>
<?php } ?>
<?php if ($post['type'] == 'conversation') { ?>
<?php echo $post{'conversation-text'}; ?>
<?php } ?>
<?php if ($post['type'] == 'video') { ?>
<!--<?php echo $post{'video-source'}; ?>-->
<?php echo $post{'video-player'}; ?>
<?php echo $post{'video-caption'}; ?>
<?php } ?>
<?php if ($post['type'] == 'conversation') { ?>
<?php echo $post{'audio-caption'}; ?>
<?php echo $post{'audio-player'}; ?>
<?php echo $post{'audio-plays'}; ?>
<?php } ?>
<div id="post-date">
<?php echo date("jS D M, H:i",strtotime($post['date'])); ?> <a href="<?php echo $post['url-with-slug']; ?>">Read on Tumblr</a>
</div>
</div>
<?php }?>
<div id="tumblr_down"></div>
<script type="text/javascript">
// load the api response
$.getJSON('http://tumblruptime.apigee.com/json?callback=?', function (response) {
// check if an api method is broken
if (!response.methods['/api/read'].up) {
$('#tumblr_down').text('Tumblr seems to not want to work with me right now. You can still try and view the posts on blog.dgovil.com. Service provided by http://tumblruptime.icodeforlove.com/apiinfo');
}
});
</script>
<div id="tumblr_postnav">
<a href="<?php echo '?offset='.($offset + 10) ?>" id="tumblr_next">Older posts</a>
<a href="<?php echo '?offset='.($offset - 10) ?>" id="tumblr_prev">Newer Posts</a>
</div>
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 PHP 解释 JSON 调用:
您会想要这样做,因为 Tumblr 的 JSON 响应似乎是非标准的。您可以将 foreach 循环更改为 for 循环或任何您喜欢的循环,以获得您想要的输出。
Use PHP to interpret the JSON call:
You'll want to do it this way b/c Tumblr's JSON response seems to be non-standard. You can change up the foreach loop to a for loop or whatever you like to get the output however you want.