PHP code to get a brief introduction to Douban on movie information

Source: Internet
Author: User

[PHP] Code

  1. Search Links
  2. function Search_link ($moviename)
  3. {
  4. Constructs a URL where max-results can change 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 link and exist in array to return
  14. for ($i = 0; $i<>
  15. $link = $entry [$i]->{' link '};
  16. for ($j = 0; $j<>
  17. $arr = (array) $link [$j];
  18. $key = Array_search ("Self", $arr);
  19. if ($key) {//Determine if key exists
  20. $STR = $arr ["@href"]. Alt=json '; Constructs a return link format
  21. Break
  22. }
  23. }
  24. $link _array[] = $str;
  25. }
  26. Return $link _array;//Back link
  27. } else
  28. echo "Not found!";
  29. }
  30. Query links and put variables into the $global array
  31. function Get_info ($urlString)
  32. {
  33. $r = new HttpRequest ($urlString, httprequest::meth_get);
  34. $response = $r->send ();
  35. $result = $r->getresponsebody ();
  36. If.. else.. Determine if open link is empty
  37. if ($obj = Json_decode ($result)) {
  38. Put the movie's information in a global array
  39. $title = $obj->{' title '};
  40. $author = $obj->{' author '};
  41. $summary = $obj->{' summary '};
  42. $ID = $obj->{' ID '};
  43. $link = $obj->{' link '};
  44. $GD = (array) $obj->{' gd:rating '};
  45. $db _array = Array ();
  46. $db = $obj->{' Db:attribute '};
  47. To put the values in Db:atribute into an array
  48. foreach ($db as $value) {
  49. $value _array = (array) ($value);
  50. $v = $value _array["@name"];
  51. $k = $value _array["\ $t"];
  52. if (array_key_exists ("@lang", $value _array)) {
  53. $lang = $value _array["@lang"];
  54. $k = $k. ' ['. $lang. '];
  55. }
  56. $db _array[$v][]= $k;
  57. }
  58. Sets the key value of the corresponding array
  59. @ $db _array_key = Array (' "movie formerly known as" ', ' "' Alias ' ', '" director "', '" screenwriter "', '" "Official website" ', ' "" IMDb link "', '" vintage ",
  60. ' Country ' ', ' ' movie type ' ', ' ' Release date ', ' ' show length ' ', ' Set Number ' ', ' ' language ' ', ' ' ' ' ' ' ' '
  61. @ $db _array_value = Array ($db _array["title"], $db _array["aka"], $db _array["director", $db _array["writer"], $db _array ["Website"],
  62. $db _array["IMDB"], $db _array["year"], $db _array["Country"], $db _array["Movie_type", $db _array["pubdate"],
  63. $db _array["Movie_duration"], $db _array["episodes"], $db _array["language"], $db _array["cast"]);
  64. @ $db _array = array_combine ($db _array_key, $db _array_value);
  65. } else{
  66. echo "Empty link!";
  67. }
  68. $info =array ("title" and $title, "author" and "$author", "Summary" and "$summary", "ID" and "$ID", "link" and "$link", "gd" = > $gd, "db_array" = $db _array);
  69. return $info;
  70. }
  71. Writing movie information to a file
  72. function Write_info ($fi, $info)
  73. {
  74. Fwrite ($fi, "******"); writing files
  75. Fwrite ($fi, "the original title of the film");
  76. foreach ($info ["title"] as $k = + $v)
  77. Fwrite ($fi, "$v \ r \ n");
  78. Fwrite ($fi, "\ r \ n");
  79. Fwrite ($fi, "producer");
  80. foreach ($info ["Author"] as $value)
  81. foreach ($value as $key)
  82. foreach ($key as $v) {
  83. Fwrite ($fi, "$v \ r \ n");
  84. }
  85. Fwrite ($fi, "\ r \ n");
  86. Db:attribute
  87. foreach ($info ["Db_array"] as $key = + $value) {
  88. if (count ($value)) {
  89. Fwrite ($fi, $key);
  90. if ($value = = $info ["Db_array"] [' "actor"]) {
  91. foreach ($value as $k = $v)
  92. Fwrite ($fi, "$v \ r \ n");
  93. } else{
  94. foreach ($value as $k = $v)
  95. Fwrite ($fi, "$v");
  96. Fwrite ($fi, "\ r \ n");
  97. }
  98. }
  99. }
  100. Fwrite ($fi, "\ r \ n");
  101. About the film
  102. Fwrite ($fi, "Introduction" \ r \ n ");
  103. foreach ($info ["Summary"] as $k = + $v) {
  104. Fwrite ($fi, "$v \ r \ n");
  105. }
  106. Fwrite ($fi, "\ r \ n");
  107. Id
  108. Fwrite ($fi, "I D" ");
  109. foreach ($info ["ID"] as $k = + $v) {
  110. Fwrite ($fi, $v);
  111. }
  112. Fwrite ($fi, "\ r \ n");
  113. Link
  114. Fwrite ($fi, "link" \ r \ n ");
  115. foreach ($info ["link"] as $value) {
  116. foreach ($value as $k = = $v) {
  117. Fwrite ($fi, "$v");
  118. }
  119. Fwrite ($fi, "\ r \ n");
  120. }
  121. Fwrite ($fi, "\ r \ n");
  122. Comments
  123. Fwrite ($fi, ' comments ');
  124. Fwrite ($fi, $info ["GD"] ["@numRaters"]);
  125. Fwrite ($fi, "\ r \ n");
  126. Fwrite ($fi, ' highest '); Fwrite ($fi, $info ["GD"] ["@max"]);
  127. Fwrite ($fi, "minimum"), Fwrite ($fi, $info ["GD"] ["@min"]);
  128. Fwrite ($fi, ' average '), fwrite ($fi, $info ["GD"] ["@average"]);
  129. Fwrite ($fi, "\ r \ n");
  130. }
  131. Find a movie in the specified folder and return to the movie name
  132. function Find_dir ($dir)
  133. {
  134. if (Is_dir ($dir)) {//Determine if the given pathname is a directory
  135. if ($fdir =opendir ($dir)) {//Open Directory
  136. while (false!== ($file =readdir ($fdir))) {//Traverse files in Directory
  137. if ($file! = "." && $file! = "...")
  138. $files []= $file; Put the found file into the array and return
  139. }
  140. Closedir ($fdir);
  141. Print_r ($file);
  142. return $files;
  143. }
  144. }
  145. }
  146. Place the generated TXT file in the specified directory
  147. function Write_to_dir ($dir, $moviename)
  148. {
  149. if (Is_dir ($dir)) {//Determine if the given pathname is a directory
  150. if ($fdir =opendir ($dir)) {//Open Directory
  151. while (false!== ($file =readdir ($fdir))) {//Traverse files in Directory
  152. if ($file! = '. ' && $file! = ' ... ') {
  153. $dirname = $dir. ' \ \ '. $file;
  154. if (Is_dir ($dirname) && $file = = $moviename) {//search for the specified folder
  155. $filename = $dirname. ' \ \ '. $moviename. ' TXT ';
  156. if (!file_exists ($filename)) {
  157. if ($fi =fopen ($filename, ' w+ ')) {//Opens the specified file, if it does not exist, creates the file and returns
  158. Closedir ($fdir);
  159. return $FI;
  160. }else
  161. echo "Fail to Open file!";
  162. //}
  163. } else
  164. echo "Dir not exists!";
  165. }
  166. }
  167. } else {
  168. Echo ' Fail to open! ';
  169. Return
  170. }
  171. } else {
  172. Echo ' Directory not exists! ';
  173. Return
  174. }
  175. }
  176. $dir = ' E:\film ';
  177. if ($movienames = @find_dir ($dir)) {//Get the name of the movie in the specified directory, search
  178. foreach ($movienames as $key = = $moviename) {
  179. if ($link _info = Search_link ($moviename)) {
  180. Search for movie names
  181. if ($fi =write_to_dir ($dir, $moviename)) {//write to the specified folder
  182. Fwrite ($fi, " \ r \ n");
  183. Fwrite ($fi, " );
  184. Fwrite ($fi, "\r\n\r\n\r\n". ※※※※※※※※※※※※※※※※※※※※※ film Information ※※※※※※※※※※※※※※※※※※※※※ '. \ r \ n ");
  185. foreach ($link _info as $k) {
  186. $info =get_info ($k); Get link Information
  187. Write_info ($fi, $info); Write information in TXT file
  188. Fwrite ($fi, "*************************************************************************************************\ r\n ");
  189. }
  190. Fclose ($FI);//write complete, close file
  191. }
  192. } else
  193. echo "empty!";
  194. }
  195. } else
  196. echo "Movies not exists!";
  197. ?>
Copy Code
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.