Introduction to getting Douban online movie information using php code

Source: Internet
Author: User
Introduction to getting Douban online movie information using php code

[PHP] code

  1. // Search links
  2. Function search_link ($ moviename)
  3. {
  4. // Construct a 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 the 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 to json format
  12. If ($ entry = @ $ obj-> {'entry '}){
  13. // Search for links and return results in an 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) {// determines whether the key exists
  18. $ Str = $ arr ["@ href"]. '? Alt = json'; // Construct the format of the returned link
  19. Break;
  20. }
  21. }
  22. $ Link_array [] = $ str;
  23. }
  24. Return $ link_array; // return link
  25. } Else
  26. Echo "Not Found! ";
  27. }
  28. // Query the link and add the variables to 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... determines whether the link is empty
  35. If ($ obj = json_decode ($ result )){
  36. // Place the video information in a 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: atri.pdf 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 video name]', '[alias]', '[Director]', '[editor ]', '[Official Website]', '[IMDB link]', '[production date ]',
  58. '[Country]', '[movie type]', '[release date]', '[display length]', '[number of sets ]', '[language]', '[performer ]');
  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 the video information to a file
  70. Function write_info ($ fi, $ info)
  71. {
  72. // Fwrite ($ fi, "*******"); write the file
  73. Fwrite ($ fi, "[original video name ]");
  74. Foreach ($ info ["title"] as $ k => $ v)
  75. Fwrite ($ fi, "$ v \ r \ n ");
  76. Fwrite ($ fi, "\ r \ n ");
  77. Fwrite ($ fi, "[buyer ]");
  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"] ['[performer]']) {
  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. // Movie introduction
  100. Fwrite ($ fi, "[introduction] \ 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. // Link
  112. Fwrite ($ fi, "[link] \ 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. // Comment
  121. Fwrite ($ fi, '[comment ]');
  122. Fwrite ($ fi, $ info ["gd"] ["@ numRaters"]);
  123. Fwrite ($ fi, "\ r \ n ");
  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, "\ r \ n ");
  128. }
  129. // Search for a movie in the specified folder and return the movie name
  130. Function find_dir ($ dir)
  131. {
  132. If (is_dir ($ dir) {// determines whether the given path name is a directory
  133. If ($ fdir = opendir ($ dir) {// open the Directory
  134. While (false! ==( $ File = readdir ($ fdir) {// traverses files in the directory
  135. If ($ file! = "." & $ File! = "..")
  136. $ Files [] = $ file; // puts the searched file into an array and returns
  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) {// determines whether the given path name is a directory
  148. If ($ fdir = opendir ($ dir) {// open the Directory
  149. While (false! ==( $ File = readdir ($ fdir) {// traverses files in the directory
  150. If ($ file! = '.' & $ File! = '..'){
  151. $ Dirname = $ dir. '\'. $ file;
  152. If (is_dir ($ dirname) & $ file ==$ moviename) {// find the specified folder
  153. $ Filename = $ dirname. '\ '.w.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) {// Obtain the movie name in the specified directory for search
  176. Foreach ($ movienames as $ key => $ moviename ){
  177. If ($ link_info = search_link ($ moviename )){
  178. // Search for the video name
  179. If ($ fi = write_to_dir ($ dir, $ moviename) {// write to the specified folder
  180. Fwrite ($ fi ," \ R \ n ");
  181. Fwrite ($ fi ,' ');
  182. Fwrite ($ fi, "\ r \ n ". ※※※※※※※※※※※※※※※※ ※※※※※'. "\ r \ n ");
  183. Foreach ($ link_info as $ k ){
  184. $ Info = get_info ($ k); // Get link information
  185. Write_info ($ fi, $ info); // write information to the txt file
  186. Fwrite ($ fi, "*************************************** **************************************** * ***************** \ r \ n ");
  187. }
  188. Fclose ($ fi); // the write is complete. close the file.
  189. }
  190. } Else
  191. Echo "Empty! ";
  192. }
  193. } Else
  194. Echo "Movies Not Exists! ";
  195. ?>

Online movies, php

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.