PHP 处理Tumblr JSON Feed

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP 处理Tumblr JSON Feed相关的知识,希望对你有一定的参考价值。

<?php
	$ch = curl_init();
	curl_setopt($ch,CURLOPT_URL,"http://demo.tumblr.com/api/read/json");
	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'];
	var_dump($posts);
	$blogroll = "";
	foreach($posts as $post){
		$blogroll .= "<p>";
		$date = date('d.m.Y',$post['unix-timestamp']);
		$blogroll .= $date."<br />";
		switch ($post['type']) {
			case 'photo':
				$blogroll .= "<a href='".$post['url-with-slug']."' target='_blank'>".str_replace('-',' ',$post['slug'])."</a>";
				break;
			
			case 'link':
				$blogroll .= "<a href='".$post['url-with-slug']."' target='_blank'>".$post['link-text']."</a><br />";
				$blogroll .= substr(strip_tags($post['link-description']),0,100)."...";
				break;

			case 'regular':
				$blogroll .= "<a href='".$post['url-with-slug']."' target='_blank'>".$post['regular-title']."</a><br />";
				$blogroll .= substr(strip_tags($post['regular-body']),0,100)."...";
				break;

			case 'quote':
				$blogroll .= "<a href='".$post['url-with-slug']."' target='_blank'>".str_replace('-',' ',$post['slug'])."</a><br />";
				$blogroll .= substr(strip_tags($post['quote-text']),0,100)."...";
				break;
				
			case 'conversation':
				if(empty($post['conversation-title'])){
					$blogroll .= "<a href='".$post['url-with-slug']."' target='_blank'>".str_replace('-',' ',$post['slug'])."</a><br />";
				}else{
					$blogroll .= "<a href='".$post['url-with-slug']."' target='_blank'>".$post['conversation-title']."</a><br />";
				}
				$blogroll .= substr(nl2br($post['conversation-text']),0,100);
				break;
				
			case 'audio':
				if(empty($post['audio-caption'])){
					$blogroll .= "<a href='".$post['url-with-slug']."' target='_blank'>".str_replace('-',' ',$post['slug'])."</a><br />";
				}else{
					$blogroll .= "<a href='".$post['url-with-slug']."' target='_blank'>".strip_tags($post['audio-caption'])."</a><br />";
				}
				break;
			
			default:
				$blogroll .= "<a href='".$post['url-with-slug']."' target='_blank'>".str_replace('-',' ',$post['slug'])."</a>";
				break;
		}
		$blogroll .= "</p>";
	}
	
	echo $blogroll;
?>

以上是关于PHP 处理Tumblr JSON Feed的主要内容,如果未能解决你的问题,请参考以下文章

进程Tumblr JSON提要

Tumblr JSON API实现

SimpleXML和PHP错误处理-Tumblr RSS示例

Tumblr API包装器

使用cURL PHP获取instagram公共JSON feed

如何向用户显示 json feed?