首頁  >  文章  >  後端開發  >  php程式碼取得豆瓣網路上電影資訊的簡介

php程式碼取得豆瓣網路上電影資訊的簡介

WBOY
WBOY原創
2016-07-25 08:42:051118瀏覽
$dir = 'E:film';

[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"=array("title"=>$title,"author"=array("title"=>$title,"author"=array("title"=>$title,"author"=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,"$vrn");
  76. fwrite($fi,"rn");
  77. fwrite($fi,"【出品人】");
  78. foreach ($info["author"] as $value)
  79. foreach ($value as $key)
  80. foreach ($key as $v) {
  81. fwrite($fi,"$vrn");
  82. }
  83. fwrite($fi,"rn" );
  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,"$vrn ");
  91. } else{
  92. foreach($value as $k=>$v)
  93. fwrite($fi,"$v ");
  94. fwrite($fi,"rn");
  95. }
  96. }
  97. }
  98. fwrite($fi,"rn");
  99. //影片簡介
  100. fwrite( $fi, "【簡介】rn");
  101. foreach ($info["summary"] as $k=>$v){
  102. fwrite($fi," $v rn");
  103. }
  104. fwrite($fi,"rn");
  105. //ID
  106. fwrite($fi,"【I D】");
  107. foreach ($info["ID"] as $k=>$v){
  108. fwrite($fi,$v);
  109. }
  110. fwrite($fi,"rn");
  111. //連結
  112. fwrite($fi,"【連結】rn");
  113. foreach ($info["link"] as $value){
  114. foreach ($value as $k=>$v){
  115. fwrite ($fi, " $v ");
  116. }
  117. fwrite($fi,"rn");
  118. }
  119. fwrite($fi,"rn");
  120. //註解
  121. fwrite($fi,'【註解】');
  122. fwrite($fi,$info["gd"]["@numRaters"]);
  123. fwrite($fi," rn");
  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,"rn");
  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'; //if(!file_exists($filename)){
  154. if($fi=fopen($filename,'w ')){ //開啟指定文件,若不存在,建立檔案並傳回
  155. closedir ($fdir);
  156. return $fi;
  157. }else
  158. echo"Fail to Open file!";
  159. //}
  160. }else
  161. echo "Dir Not Exists!";
  162. }
  163. }
  164. } else {
  165. echo 'Fail to Open!';
  166. return;
  167. }
  168. }
  169. }
  170. }
  171. }
  172. }
  173. }
  174. }
  175. }
  176. }
  177. }
  178. }
  179. }
  180. }
  181. }
  182. }
  183. }
  184. }
  185. }
  186. }
  187. }
  188. }
  189. }
  190. }
  191. }
  192. }
  193. }
  194. }
  195. }
  196. }
  197. }
} else {
echo 'Directory Not Exists!'; return; }
}
if($movienames = @ find_dir($dir)){ //在指定目錄中取得電影名,進行搜尋
foreach ($movienames as $key=>$moviename){
if($link_info = search_link($moviename)){
//搜尋影片名稱
if ($fi=write_to_dir($dir,$moviename)){ //寫入指定資料夾
fwrite($fi,"rn" );
fwrite($fi,'');
fwrite($fi,"rnrnrn".'※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※‴ ※ 影片資訊※※※※※※※※※※※※※※※※※※※※※'."rn");
for🎜>> 商. info=get_info($k); //取得連結資訊 write_info($fi,$info); //在txt檔案中寫入訊息 fwrite($fi,"******* ************************************************** ****************************************rn"); } fclose($fi);//寫入完成,關閉檔案 } } else echo "Empty!"; }} else echo "Movies Not Existss !";?>複製程式碼 網路電影, php
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn