>  기사  >  백엔드 개발  >  php代码获取豆瓣网上电影信息的简介

php代码获取豆瓣网上电影信息的简介

WBOY
WBOY원래의
2016-07-25 08:42:051118검색

[PHP]代码

  1. //搜索链接
  2. function search_link($moviename)
  3. {
  4. //构造url,其中max-results可根据需要更改
  5. $urlString = 'http://api.douban.com/movie/subjects?q='.$moviename.'&start-index=1&max-results=1&alt=json';
  6. //print_r($urlString);
  7. $urlString=mb_convert_encoding($urlString, "UTF-8", "GBK");//将Url转换为utf-8编码
  8. $r = new HttpRequest($urlString,HttpRequest::METH_GET);//请求
  9. $response = $r->send();
  10. $result = $r->getResponseBody();
  11. $obj = json_decode($result);//解析成json格式
  12. if($entry = @$obj->{'entry'}){
  13. //搜索链接并存在数组中返回
  14. for($i = 0;$i $link=$entry[$i]->{'link'};
  15. for($j = 0;$j $arr = (array)$link[$j];
  16. $key = array_search("self",$arr);
  17. if($key){ //判断key是否存在
  18. $str = $arr["@href"].'?alt=json'; //构造返回链接的格式
  19. break;
  20. }
  21. }
  22. $link_array[] = $str;
  23. }
  24. return $link_array;//返回链接
  25. } else
  26. echo"Not Found!";
  27. }
  28. //查询链接并将各变量放入$GLOBAL 数组
  29. function get_info($urlString)
  30. {
  31. $r = new HttpRequest($urlString,HttpRequest::METH_GET);
  32. $response = $r->send();
  33. $result = $r->getResponseBody();
  34. //if..else.. 判断打开链接是否为空
  35. if ($obj = json_decode($result)){
  36. //将影片的信息放在全局数组中
  37. $title = $obj->{'title'};
  38. $author = $obj->{'author'};
  39. $summary = $obj->{'summary'};
  40. $ID=$obj->{'id'};
  41. $link = $obj->{'link'};
  42. $gd = (array)$obj->{'gd:rating'};
  43. $db_array = array();
  44. $db = $obj->{'db:attribute'};
  45. //将db:atribute中的值放进数组
  46. foreach ($db as $value){
  47. $value_array = (array)($value);
  48. $v = $value_array["@name"];
  49. $k = $value_array["\$t"];
  50. if (array_key_exists("@lang",$value_array)){
  51. $lang=$value_array["@lang"];
  52. $k=$k.'['.$lang.']';
  53. }
  54. $db_array[$v][]=$k;
  55. }
  56. //设定对应数组的键值
  57. @$db_array_key = array('【影片原名】','【别 名】','【导 演】','【编 剧】','【官方网站】','【IMDB链接】','【出品年代】',
  58. '【国 家】','【电影类型】','【上映日期】','【放映长度】','【集 数】','【语 言】','【演 员】');
  59. @$db_array_value = array($db_array["title"],$db_array["aka"],$db_array["director"],$db_array["writer"],$db_array["website"],
  60. $db_array["imdb"],$db_array["year"],$db_array["country"],$db_array["movie_type"],$db_array["pubdate"],
  61. $db_array["movie_duration"],$db_array["episodes"],$db_array["language"],$db_array["cast"]);
  62. @$db_array = array_combine($db_array_key,$db_array_value);
  63. } else{
  64. echo "Empty link!";
  65. }
  66. $info=array("title"=>$title,"author"=>$author,"summary"=>$summary,"ID"=>$ID,"link"=>$link,"gd"=>$gd,"db_array"=>$db_array);
  67. return $info;
  68. }
  69. //将影片信息写入文件
  70. function write_info($fi,$info)
  71. {
  72. //fwrite($fi, "******");写入文件
  73. fwrite($fi, "【影片原名】");
  74. foreach ($info["title"] as $k=>$v)
  75. fwrite($fi,"$v\r\n");
  76. fwrite($fi,"\r\n");
  77. fwrite($fi,"【出 品 人】");
  78. foreach ($info["author"] as $value)
  79. foreach ($value as $key)
  80. foreach ($key as $v) {
  81. fwrite($fi,"$v\r\n");
  82. }
  83. fwrite($fi,"\r\n");
  84. //db:attribute
  85. foreach ($info["db_array"] as $key=>$value){
  86. if(count($value)){
  87. fwrite($fi,$key);
  88. if($value==$info["db_array"]['【演 员】']){
  89. foreach($value as $k=>$v)
  90. fwrite($fi,"$v\r\n ");
  91. } else{
  92. foreach($value as $k=>$v)
  93. fwrite($fi,"$v ");
  94. fwrite($fi,"\r\n");
  95. }
  96. }
  97. }
  98. fwrite($fi,"\r\n");
  99. //影片简介
  100. fwrite($fi, "【简 介】\r\n");
  101. foreach ($info["summary"] as $k=>$v){
  102. fwrite($fi," $v \r\n");
  103. }
  104. fwrite($fi,"\r\n");
  105. //ID
  106. fwrite($fi,"【I D】");
  107. foreach ($info["ID"] as $k=>$v){
  108. fwrite($fi,$v);
  109. }
  110. fwrite($fi,"\r\n");
  111. //链接
  112. fwrite($fi,"【链 接】\r\n");
  113. foreach ($info["link"] as $value){
  114. foreach ($value as $k=>$v){
  115. fwrite($fi, " $v ");
  116. }
  117. fwrite($fi,"\r\n");
  118. }
  119. fwrite($fi,"\r\n");
  120. //评论
  121. fwrite($fi,'【评 论】');
  122. fwrite($fi,$info["gd"]["@numRaters"]);
  123. fwrite($fi,"\r\n");
  124. fwrite($fi,'【最高】'); fwrite($fi,$info["gd"]["@max"]);
  125. fwrite($fi," 【最低】");fwrite($fi,$info["gd"]["@min"]);
  126. fwrite($fi,' 【平均】');fwrite($fi, $info["gd"]["@average"]);
  127. fwrite($fi,"\r\n");
  128. }
  129. //在指定的文件夹中寻找电影并返回电影名
  130. function find_dir($dir)
  131. {
  132. if(is_dir($dir)){ //判断所给路径名是否为目录
  133. if($fdir=opendir($dir)){ //打开目录
  134. while(false!==($file=readdir($fdir))){ //遍历目录中的文件
  135. if ($file != "." && $file != "..")
  136. $files[]=$file; //将寻找到的文件放入数组并返回
  137. }
  138. closedir($fdir);
  139. print_r($file);
  140. return $files;
  141. }
  142. }
  143. }
  144. //将生成的txt文件放在指定的目录下
  145. function write_to_dir($dir,$moviename)
  146. {
  147. if (is_dir($dir)){ //判断所给路径名是否为目录
  148. if($fdir=opendir($dir)){ //打开目录
  149. while(false!==($file=readdir($fdir))){ //遍历目录中的文件
  150. if ($file != '.' && $file != '..') {
  151. $dirname=$dir.'\\'.$file;
  152. if (is_dir($dirname)&&$file==$moviename) { //寻找指定的文件夹
  153. $filename=$dirname.'\\'.$moviename.'.txt';
  154. //if(!file_exists($filename)){
  155. if($fi=fopen($filename,'w+')){ //打开指定文件,若不存在,建立文件并返回
  156. closedir($fdir);
  157. return $fi;
  158. }else
  159. echo"Fail to Open file!";
  160. //}
  161. } else
  162. echo "Dir Not Exists!";
  163. }
  164. }
  165. } else {
  166. echo 'Fail to Open!';
  167. return;
  168. }
  169. } else {
  170. echo 'Directory Not Exists!';
  171. return;
  172. }
  173. }
  174. $dir = 'E:\film';
  175. if($movienames = @find_dir($dir)){ //在指定目录中获得电影名,进行搜索
  176. foreach ($movienames as $key=>$moviename){
  177. if($link_info = search_link($moviename)){
  178. //搜索影片名
  179. if ($fi=write_to_dir($dir,$moviename)){ //写入指定文件夹
  180. fwrite($fi,"\r\n");
  181. fwrite($fi,'');
  182. fwrite($fi,"\r\n\r\n\r\n".'※※※※※※※※※※※※※※※※※※※※※ 影片信息 ※※※※※※※※※※※※※※※※※※※※※'."\r\n");
  183. foreach ($link_info as $k){
  184. $info=get_info($k); //获取链接信息
  185. write_info($fi,$info); //在txt文件中写入信息
  186. fwrite($fi,"*************************************************************************************************\r\n");
  187. }
  188. fclose($fi);//写入完成,关闭文件
  189. }
  190. } else
  191. echo "Empty!";
  192. }
  193. } else
  194. echo "Movies Not Exists!";
  195. ?>
复制代码
网上电影, php


성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
이전 기사:php获取汉字首字母 다음 기사:php实现telnet功能