[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 $link=$entry[$i]->{'link'};
- for($j = 0;$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"=array("title"=>$title,"author"=array("title"=>$title,"author"=array("title"=>$title,"author"=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,"$vrn");
- fwrite($fi,"rn");
-
- fwrite($fi,"【出品人】");
- foreach ($info["author"] as $value)
- foreach ($value as $key)
- foreach ($key as $v) {
- fwrite($fi,"$vrn");
- }
- fwrite($fi,"rn" );
-
- //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,"$vrn ");
- } else{
- foreach($value as $k=>$v)
- fwrite($fi,"$v ");
- fwrite($fi,"rn");
- }
- }
- }
- fwrite($fi,"rn");
-
- //影片簡介
- fwrite( $fi, "【簡介】rn");
- foreach ($info["summary"] as $k=>$v){
- fwrite($fi," $v rn");
- }
- fwrite($fi,"rn");
-
- //ID
- fwrite($fi,"【I D】");
- foreach ($info["ID"] as $k=>$v){
- fwrite($fi,$v);
- }
- fwrite($fi,"rn");
-
- //連結
- fwrite($fi,"【連結】rn");
- foreach ($info["link"] as $value){
- foreach ($value as $k=>$v){
- fwrite ($fi, " $v ");
- }
- fwrite($fi,"rn");
- }
- fwrite($fi,"rn");
-
- //註解
- fwrite($fi,'【註解】');
- fwrite($fi,$info["gd"]["@numRaters"]);
- fwrite($fi," rn");
- fwrite($fi,'【最高】'); fwrite($fi,$info["gd"]["@max"]);
- fwrite($fi," 【最低】");fwrite($fi,$info["gd"]["@min"]);
- fwrite($fi,' 【平均】');fwrite($fi, $info["gd" ]["@average"]);
- fwrite($fi,"rn");
- }
- //在指定的資料夾中尋找影片並傳回電影名稱
- 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';