用来获取豆瓣网上电影信息的简介
[PHP]代码
<?php //搜索链接 function search_link($moviename) { //构造url,其中max-results可根据需要更改 $urlString = 'http://api.douban.com/movie/subjects?q='.$moviename.'&start-index=1&max-results=1&alt=json'; //print_r($urlString); $urlString=mb_convert_encoding($urlString, "UTF-8", "GBK");//将Url转换为utf-8编码 $r = new HttpRequest($urlString,HttpRequest::METH_GET);//请求 $response = $r->send(); $result = $r->getResponseBody(); $obj = json_decode($result);//解析成json格式 if($entry = @$obj->{'entry'}){ //搜索链接并存在数组中返回 for($i = 0;$i<sizeof($entry);$i++){ $link=$entry[$i]->{'link'}; for($j = 0;$j<sizeof($link);$j++){ $arr = (array)$link[$j]; $key = array_search("self",$arr); if($key){ //判断key是否存在 $str = $arr["@href"].'?alt=json'; //构造返回链接的格式 break; } } $link_array[] = $str; } return $link_array;//返回链接 } else echo"Not Found!"; } //查询链接并将各变量放入$GLOBAL 数组 function get_info($urlString) { $r = new HttpRequest($urlString,HttpRequest::METH_GET); $response = $r->send(); $result = $r->getResponseBody(); //if..else.. 判断打开链接是否为空 if ($obj = json_decode($result)){ //将影片的信息放在全局数组中 $title = $obj->{'title'}; $author = $obj->{'author'}; $summary = $obj->{'summary'}; $ID=$obj->{'id'}; $link = $obj->{'link'}; $gd = (array)$obj->{'gd:rating'}; $db_array = array(); $db = $obj->{'db:attribute'}; //将db:atribute中的值放进数组 foreach ($db as $value){ $value_array = (array)($value); $v = $value_array["@name"]; $k = $value_array["\$t"]; if (array_key_exists("@lang",$value_array)){ $lang=$value_array["@lang"]; $k=$k.'['.$lang.']'; } $db_array[$v][]=$k; } //设定对应数组的键值 @$db_array_key = array('【影片原名】','【别 名】','【导 演】','【编 剧】','【官方网站】','【IMDB链接】','【出品年代】', '【国 家】','【电影类型】','【上映日期】','【放映长度】','【集 数】','【语 言】','【演 员】'); @$db_array_value = array($db_array["title"],$db_array["aka"],$db_array["director"],$db_array["writer"],$db_array["website"], $db_array["imdb"],$db_array["year"],$db_array["country"],$db_array["movie_type"],$db_array["pubdate"], $db_array["movie_duration"],$db_array["episodes"],$db_array["language"],$db_array["cast"]); @$db_array = array_combine($db_array_key,$db_array_value); } else{ echo "Empty link!"; } $info=array("title"=>$title,"author"=>$author,"summary"=>$summary,"ID"=>$ID,"link"=>$link,"gd"=>$gd,"db_array"=>$db_array); return $info; } //将影片信息写入文件 function write_info($fi,$info) { //fwrite($fi, "******");写入文件 fwrite($fi, "【影片原名】"); foreach ($info["title"] as $k=>$v) fwrite($fi,"$v\r\n"); fwrite($fi,"\r\n"); fwrite($fi,"【出 品 人】"); foreach ($info["author"] as $value) foreach ($value as $key) foreach ($key as $v) { fwrite($fi,"$v\r\n"); } fwrite($fi,"\r\n"); //db:attribute foreach ($info["db_array"] as $key=>$value){ if(count($value)){ fwrite($fi,$key); if($value==$info["db_array"]['【演 员】']){ foreach($value as $k=>$v) fwrite($fi,"$v\r\n "); } else{ foreach($value as $k=>$v) fwrite($fi,"$v "); fwrite($fi,"\r\n"); } } } fwrite($fi,"\r\n"); //影片简介 fwrite($fi, "【简 介】\r\n"); foreach ($info["summary"] as $k=>$v){ fwrite($fi," $v \r\n"); } fwrite($fi,"\r\n"); //ID fwrite($fi,"【I D】"); foreach ($info["ID"] as $k=>$v){ fwrite($fi,$v); } fwrite($fi,"\r\n"); //链接 fwrite($fi,"【链 接】\r\n"); foreach ($info["link"] as $value){ foreach ($value as $k=>$v){ fwrite($fi, " $v "); } fwrite($fi,"\r\n"); } fwrite($fi,"\r\n"); //评论 fwrite($fi,'【评 论】'); fwrite($fi,$info["gd"]["@numRaters"]); fwrite($fi,"\r\n"); fwrite($fi,'【最高】'); fwrite($fi,$info["gd"]["@max"]); fwrite($fi," 【最低】");fwrite($fi,$info["gd"]["@min"]); fwrite($fi,' 【平均】');fwrite($fi, $info["gd"]["@average"]); fwrite($fi,"\r\n"); } //在指定的文件夹中寻找电影并返回电影名 function find_dir($dir) { if(is_dir($dir)){ //判断所给路径名是否为目录 if($fdir=opendir($dir)){ //打开目录 while(false!==($file=readdir($fdir))){ //遍历目录中的文件 if ($file != "." && $file != "..") $files[]=$file; //将寻找到的文件放入数组并返回 } closedir($fdir); print_r($file); return $files; } } } //将生成的txt文件放在指定的目录下 function write_to_dir($dir,$moviename) { if (is_dir($dir)){ //判断所给路径名是否为目录 if($fdir=opendir($dir)){ //打开目录 while(false!==($file=readdir($fdir))){ //遍历目录中的文件 if ($file != '.' && $file != '..') { $dirname=$dir.'\\'.$file; if (is_dir($dirname)&&$file==$moviename) { //寻找指定的文件夹 $filename=$dirname.'\\'.$moviename.'.txt'; //if(!file_exists($filename)){ if($fi=fopen($filename,'w+')){ //打开指定文件,若不存在,建立文件并返回 closedir($fdir); return $fi; }else echo"Fail to Open file!"; //} } else echo "Dir Not Exists!"; } } } else { echo 'Fail to Open!'; return; } } else { echo 'Directory Not Exists!'; return; } } $dir = 'E:\film'; 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,"<Resources>\r\n"); fwrite($fi,'<MesageBody>'); fwrite($fi,"\r\n\r\n\r\n".'※※※※※※※※※※※※※※※※※※※※※ 影片信息 ※※※※※※※※※※※※※※※※※※※※※'."\r\n"); foreach ($link_info as $k){ $info=get_info($k); //获取链接信息 write_info($fi,$info); //在txt文件中写入信息 fwrite($fi,"*************************************************************************************************\r\n"); } fclose($fi);//写入完成,关闭文件 } } else echo "Empty!"; } } else echo "Movies Not Exists!"; ?>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

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,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.
