豆瓣API之旅(1)

这段时间闲来无事,于是想通过做点小东西自学一下php,mysql啥的。

最早关注的是淘宝API,现在先做一点豆瓣API来玩玩。

目标:展示豆瓣上热门的影片、音乐、书籍。比如给出影片的片名、图片、导演、主演、简介等。

本次目标:给定3个影片id,显示影片片名、图片、导演、主演、简介。

本次版本效果
本次用到的API

文件:

1)util.php 这是我从淘宝的phpdemo中提出来的一个文件,用来把json decode出来的对象转成数组

2)20100119.php 用户访问文件

3)JSON.php 一个json的php库,在这里你可以获得它的文件包

代码:
util.php 部分

1
2
3
4
5
6
7
8
9
10
11
function get_object_vars_final($obj){
	if(is_object($obj)){
		$obj=get_object_vars($obj);
	}
	if(is_array($obj)){
		foreach ($obj as $key=>$value){
			$obj[$key]=get_object_vars_final($value);
		}
	}
	return $obj;
}

20100119.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
<?php
require_once 'JSON.php';
require_once 'util.php';
 
 
 
$json = new Services_JSON();
$movieUrl = 'http://api.douban.com/movie/subject/';
$requestEnd = '?alt=json';
 
$popularMovie = array('1652587', '3077791', '1892531');
?>
<table border="1" width="100%">
	<tr>
		<td align="center">
			片名
		</td>
		<td align="center">
			图片
		</td>
		<td align="center">
			导演
		</td>
		<td align="center">
			主演
		</td>
		<td align="center">
			简介
		</td>
	</tr>
	<?php
	foreach ($popularMovie as $movie) {
		$contents = file_get_contents($movieUrl.$movie.$requestEnd);
		$value = $json->decode($contents);
		$contents = get_object_vars_final($value);
	?>
	<tr>
		<td align="center">
			<a href=<?php echo $contents['link'][1]['@href']?>><?php echo $contents['title']['$t'];?></a>
		</td>
		<td align="center">
			<img src=<?php echo $contents['link'][2]['@href']?> alt="pic from douban">
		</td>
		<td align="center">
			<?php echo $contents['author'][0]['name']['$t'];?>
		</td>
		<td align="center">
			<?php echo $contents['db:attribute'][10]['$t'];?>
		</td>
		<td align="center">
			<?php echo $contents['summary']['$t'];?>
		</td>
	</tr>
	<?php
		}
	?>
</table>

代码下载

2010年1月19日 | 归档于 豆瓣API之旅
标签: , ,
本文目前尚无任何评论.

发表评论

XHTML: 您可以使用这些标签: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">