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

PHP code to obtain Douban online movie information introduction

Jul 25, 2016 am 08:42 AM

[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 of this Website
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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Explain JSON Web Tokens (JWT) and their use case in PHP APIs. Apr 05, 2025 am 12:04 AM

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

Explain the concept of late static binding in PHP. Explain the concept of late static binding in PHP. Mar 21, 2025 pm 01:33 PM

Article discusses late static binding (LSB) in PHP, introduced in PHP 5.3, allowing runtime resolution of static method calls for more flexible inheritance.Main issue: LSB vs. traditional polymorphism; LSB's practical applications and potential perfo

Framework Security Features: Protecting against vulnerabilities. Framework Security Features: Protecting against vulnerabilities. Mar 28, 2025 pm 05:11 PM

Article discusses essential security features in frameworks to protect against vulnerabilities, including input validation, authentication, and regular updates.

Customizing/Extending Frameworks: How to add custom functionality. Customizing/Extending Frameworks: How to add custom functionality. Mar 28, 2025 pm 05:12 PM

The article discusses adding custom functionality to frameworks, focusing on understanding architecture, identifying extension points, and best practices for integration and debugging.

How to send a POST request containing JSON data using PHP's cURL library? How to send a POST request containing JSON data using PHP's cURL library? Apr 01, 2025 pm 03:12 PM

Sending JSON data using PHP's cURL library In PHP development, it is often necessary to interact with external APIs. One of the common ways is to use cURL library to send POST�...

Describe the SOLID principles and how they apply to PHP development. Describe the SOLID principles and how they apply to PHP development. Apr 03, 2025 am 12:04 AM

The application of SOLID principle in PHP development includes: 1. Single responsibility principle (SRP): Each class is responsible for only one function. 2. Open and close principle (OCP): Changes are achieved through extension rather than modification. 3. Lisch's Substitution Principle (LSP): Subclasses can replace base classes without affecting program accuracy. 4. Interface isolation principle (ISP): Use fine-grained interfaces to avoid dependencies and unused methods. 5. Dependency inversion principle (DIP): High and low-level modules rely on abstraction and are implemented through dependency injection.

What exactly is the non-blocking feature of ReactPHP? How to handle its blocking I/O operations? What exactly is the non-blocking feature of ReactPHP? How to handle its blocking I/O operations? Apr 01, 2025 pm 03:09 PM

An official introduction to the non-blocking feature of ReactPHP in-depth interpretation of ReactPHP's non-blocking feature has aroused many developers' questions: "ReactPHPisnon-blockingbydefault...

See all articles