search
Homephp教程PHP源码用来获取豆瓣网上电影信息的简介

[PHP]代码   

<?php
//搜索链接
function search_link($moviename)
{
		
        //构造url,其中max-results可根据需要更改
        $urlString = &#39;http://api.douban.com/movie/subjects?q=&#39;.$moviename.&#39;&start-index=1&max-results=1&alt=json&#39;;
		//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->{&#39;entry&#39;}){
				//搜索链接并存在数组中返回
				for($i = 0;$i<sizeof($entry);$i++){
						$link=$entry[$i]->{&#39;link&#39;}; 
						for($j = 0;$j<sizeof($link);$j++){
							  $arr = (array)$link[$j];
							  $key = array_search("self",$arr);
							  if($key){									//判断key是否存在
								  $str = $arr["@href"].&#39;?alt=json&#39;; //构造返回链接的格式
								  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->{&#39;title&#39;};
			$author = $obj->{&#39;author&#39;};
			$summary = $obj->{&#39;summary&#39;};
			$ID=$obj->{&#39;id&#39;};
			$link = $obj->{&#39;link&#39;};  
			$gd = (array)$obj->{&#39;gd:rating&#39;};	
			$db_array = array();
			
			$db = $obj->{&#39;db:attribute&#39;};
			//将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.&#39;[&#39;.$lang.&#39;]&#39;;
				}		
			    $db_array[$v][]=$k; 
			}
            //设定对应数组的键值
			@$db_array_key = array(&#39;【影片原名】&#39;,&#39;【别    名】&#39;,&#39;【导    演】&#39;,&#39;【编    剧】&#39;,&#39;【官方网站】&#39;,&#39;【IMDB链接】&#39;,&#39;【出品年代】&#39;,
			                   &#39;【国    家】&#39;,&#39;【电影类型】&#39;,&#39;【上映日期】&#39;,&#39;【放映长度】&#39;,&#39;【集    数】&#39;,&#39;【语    言】&#39;,&#39;【演    员】&#39;);
			@$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"][&#39;【演    员】&#39;]){
							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,&#39;【评    论】&#39;); 
            fwrite($fi,$info["gd"]["@numRaters"]);
			fwrite($fi,"\r\n"); 
			fwrite($fi,&#39;【最高】&#39;);  fwrite($fi,$info["gd"]["@max"]);
			fwrite($fi,"  【最低】");fwrite($fi,$info["gd"]["@min"]);
			fwrite($fi,&#39;  【平均】&#39;);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 != &#39;.&#39; && $file != &#39;..&#39;) {  
					$dirname=$dir.&#39;\\&#39;.$file;
					if (is_dir($dirname)&&$file==$moviename) {       			//寻找指定的文件夹
						$filename=$dirname.&#39;\\&#39;.$moviename.&#39;.txt&#39;; 
                        //if(!file_exists($filename)){						
							if($fi=fopen($filename,&#39;w+&#39;)){                       //打开指定文件,若不存在,建立文件并返回
								closedir($fdir);
								return $fi;	  
							}else
								echo"Fail to Open file!";	
						//}								
					} else
							echo "Dir Not Exists!";				
				}	
			}  
		} else {
						echo &#39;Fail to Open!&#39;;	
						return;
		}
	}   else {
	            echo &#39;Directory Not Exists!&#39;;
				return;
	}
}

$dir = &#39;E:\film&#39;;
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,&#39;<MesageBody>&#39;);
				fwrite($fi,"\r\n\r\n\r\n".&#39;※※※※※※※※※※※※※※※※※※※※※ 影片信息 ※※※※※※※※※※※※※※※※※※※※※&#39;."\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!";
?>

                   

                   

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
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace("&nbsp;","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么读取字符串后几个字符php怎么读取字符串后几个字符Apr 22, 2022 pm 08:31 PM

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

php怎么判断有没有小数点php怎么判断有没有小数点Apr 20, 2022 pm 08:12 PM

php判断有没有小数点的方法:1、使用“strpos(数字字符串,'.')”语法,如果返回小数点在字符串中第一次出现的位置,则有小数点;2、使用“strrpos(数字字符串,'.')”语句,如果返回小数点在字符串中最后一次出现的位置,则有。

See all articles

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

Repo: How To Revive Teammates
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor