Home  >  Article  >  Backend Development  >  PHP code to obtain Douban online movie information introduction

PHP code to obtain Douban online movie information introduction

WBOY
WBOYOriginal
2016-07-25 08:42:051078browse

[PHP] code

  1. //Search link
  2. function search_link($moviename)
  3. {
  4. //Construct url, where max-results can be changed as needed
  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");//Convert Url to utf-8 encoding
  8. $r = new HttpRequest($urlString,HttpRequest::METH_GET);//Request
  9. $response = $r->send();
  10. $ result = $r->getResponseBody();
  11. $obj = json_decode($result);//Parse into json format
  12. if($entry = @$obj->{'entry'}){
  13. // Search the link and return it in the array
  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){ //Determine whether the key exists
  18. $str = $arr["@href"].'?alt=json'; //Construct the format of the return link
  19. break;
  20. }
  21. }
  22. $link_array[] = $ str;
  23. }
  24. return $link_array;//Return to the link
  25. } else
  26. echo "Not Found!";
  27. }
  28. //Query the link and put each variable into the $GLOBAL array
  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.. Determine whether the open link is empty
  35. if ($obj = json_decode($result)){
  36. //Put the video information in the global array
  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. //Put the value in db:atribute into the array
  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. //Set the key value of the corresponding array
  57. @$db_array_key = array('[Original name of the video 】','[Alias]','[Director]','[Screenwriter]','[Official Website]','[IMDB Link]','[Year of Production]',
  58. '[Country]',' [Movie Type]','[Release Date]','[Screen Length]','[Number of Episodes]','[Language]','[Actor]');
  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. //Write movie information to the file
  70. function write_info($fi,$info)
  71. {
  72. //fwrite($fi, "******");Write the file
  73. fwrite($fi, "【 Original title of the video】");
  74. foreach ($info["title"] as $k=>$v)
  75. fwrite($fi,"$vrn");
  76. fwrite($fi,"rn");
  77. fwrite($fi,"[producer]");
  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"]['[actor]']){
  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. //Video introduction
  100. fwrite($fi, "[Introduction]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. //Link
  112. fwrite($fi,"[Link]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. //Comment
  121. fwrite($fi, '[Comment]');
  122. fwrite($fi,$info["gd"]["@numRaters"]);
  123. fwrite($fi,"rn");
  124. fwrite($fi,'[Highest]' ); fwrite($fi,$info["gd"]["@max"]);
  125. fwrite($fi," [minimum]");fwrite($fi,$info["gd"]["@ min"]);
  126. fwrite($fi,' [Average]');fwrite($fi, $info["gd"]["@average"]);
  127. fwrite($fi,"rn");
  128. }
  129. //Find the movie in the specified folder and return the movie name
  130. function find_dir($dir)
  131. {
  132. if(is_dir($dir)){ //Judge whether the given path name is a directory
  133. if($fdir =opendir($dir)){ //Open the directory
  134. while(false!==($file=readdir($fdir))){ //Traverse the files in the directory
  135. if ($file != "." && $ file != "..")
  136. $files[]=$file; //Put the found files into the array and return
  137. }
  138. closedir($fdir);
  139. print_r($file);
  140. return $files;
  141. }
  142. }
  143. }
  144. //Place the generated txt file in the specified directory
  145. function write_to_dir($dir,$moviename)
  146. {
  147. if (is_dir($dir)){ //Determine whether the given path name is For the directory
  148. if($fdir=opendir($dir)){ //Open the directory
  149. while(false!==($file=readdir($fdir))){ //Traverse the files in the directory
  150. if ($file != '.' && $file != '..') {
  151. $dirname=$dir.'\'.$file;
  152. if (is_dir($dirname)&&$file==$moviename) { //Find Specified folder
  153. $filename=$dirname.'\'.$moviename.'.txt';
  154. //if(!file_exists($filename)){
  155. if($fi=fopen($filename,'w+' )){ //Open the specified file. If it does not exist, create the file and return
  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)){ //Get the movie name in the specified directory and search
  176. foreach ($movienames as $key=> $moviename){
  177. if($link_info = search_link($moviename)){
  178. //Search for movie name
  179. if ($fi=write_to_dir($dir,$moviename)){ //Write to the specified folder
  180. fwrite( $fi,"rn");
  181. fwrite($fi,'');
  182. fwrite($fi,"rnrnrn".'※※※※※※※※※※※※※ ※※※※※※※※Video information※※※※※※※※※※※※※※※※※※※※※'."rn");
  183. foreach ($link_info as $k){
  184. $info=get_info($k); //Get link information
  185. write_info($fi,$info); //Write information in txt file
  186. fwrite($fi,"********** *************************************************** *************************************rn");
  187. }
  188. fclose($fi); //Writing completed, close the file
  189. }
  190. } else
  191. echo "Empty!";
  192. }
  193. } else
  194. echo "Movies Not Exists!";
  195. ?>
Copy code
Online movies, php


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn