1<?php
  2//搜索链接
  3function search_link($moviename)
  4{
  5        //构造url,其中max-results可根据需要更改
  6        $urlString = 'http://api.douban.com/movie/subjects?q='.$moviename.'&start-index=1&max-results=1&alt=json';
  7        //print_r($urlString);
  8        $urlString=mb_convert_encoding($urlString, "UTF-8", "GBK");//将Url转换为utf-8编码
  9        $r = new HttpRequest($urlString,HttpRequest::METH_GET);//请求
 10        $response = $r->send();
 11        $result = $r->getResponseBody();
 12        $obj = json_decode($result);//解析成json格式
 13        if($entry = @$obj->{'entry'}){
 14                //搜索链接并存在数组中返回
 15                for($i = 0;$i<sizeof($entry);$i++){
 16                        $link=$entry[$i]->{'link'};
 17                        for($j = 0;$j<sizeof($link);$j++){
 18                              $arr = (array)$link[$j];
 19                              $key = array_search("self",$arr);
 20                              if($key){                                 //判断key是否存在
 21                                  $str = $arr["@href"].'?alt=json'; //构造返回链接的格式
 22                                  break;
 23                              }
 24                        }
 25                        $link_array[] = $str;
 26                }
 27                return $link_array;//返回链接
 28        } else
 29                echo"Not Found!";
 30}
 31//查询链接并将各变量放入$GLOBAL 数组
 32function get_info($urlString)
 33{
 34        $r = new HttpRequest($urlString,HttpRequest::METH_GET);
 35        $response = $r->send();
 36        $result = $r->getResponseBody();
 37        //if..else.. 判断打开链接是否为空
 38        if ($obj = json_decode($result)){
 39            //将影片的信息放在全局数组中
 40            $title = $obj->{'title'};
 41            $author = $obj->{'author'};
 42            $summary = $obj->{'summary'};
 43            $ID=$obj->{'id'};
 44            $link = $obj->{'link'};
 45            $gd = (array)$obj->{'gd:rating'};
 46            $db_array = array();
 47            $db = $obj->{'db:attribute'};
 48            //将db:atribute中的值放进数组
 49            foreach ($db as $value){
 50                $value_array = (array)($value);
 51                $v = $value_array["@name"];
 52                $k = $value_array["$t"];
 53                if (array_key_exists("@lang",$value_array)){
 54                    $lang=$value_array["@lang"];
 55                    $k=$k.'['.$lang.']';
 56                }
 57                $db_array[$v][]=$k;
 58            }
 59            //设定对应数组的键值
 60            @$db_array_key = array('【影片原名】','【别    名】','【导    演】','【编    剧】','【官方网站】','【IMDB链接】','【出品年代】',
 61                               '【国    家】','【电影类型】','【上映日期】','【放映长度】','【集    数】','【语    言】','【演    员】');
 62            @$db_array_value = array($db_array["title"],$db_array["aka"],$db_array["director"],$db_array["writer"],$db_array["website"],
 63                                 $db_array["imdb"],$db_array["year"],$db_array["country"],$db_array["movie_type"],$db_array["pubdate"],
 64                                 $db_array["movie_duration"],$db_array["episodes"],$db_array["language"],$db_array["cast"]);
 65            @$db_array = array_combine($db_array_key,$db_array_value);
 66        }   else{
 67                echo "Empty link!";
 68        }
 69        $info=array("title"=>$title,"author"=>$author,"summary"=>$summary,"ID"=>$ID,"link"=>$link,"gd"=>$gd,"db_array"=>$db_array);
 70        return $info;
 71}
 72//将影片信息写入文件
 73function write_info($fi,$info)
 74{
 75            //fwrite($fi, "******");写入文件
 76            fwrite($fi, "【影片原名】");
 77            foreach ($info["title"] as $k=>$v)
 78                    fwrite($fi,"$vrn");
 79            fwrite($fi,"rn");
 80            fwrite($fi,"【出 品 人】");
 81              foreach ($info["author"] as $value)
 82                  foreach ($value as $key)
 83                    foreach ($key as $v) {
 84                      fwrite($fi,"$vrn");
 85                    }
 86            fwrite($fi,"rn");
 87            //db:attribute
 88            foreach ($info["db_array"] as $key=>$value){
 89                if(count($value)){
 90                        fwrite($fi,$key);
 91                        if($value==$info["db_array"]['【演    员】']){
 92                            foreach($value as $k=>$v)
 93                                fwrite($fi,"$vrn            ");
 94                        }   else{
 95                                foreach($value as $k=>$v)
 96                                    fwrite($fi,"$v  ");
 97                                fwrite($fi,"rn");
 98                        }
 99                }
100            }
101            fwrite($fi,"rn");
102            //影片简介
103            fwrite($fi, "【简    介】rn");
104            foreach ($info["summary"] as $k=>$v){
105                fwrite($fi,"   $v rn");
106            }
107            fwrite($fi,"rn");
108            //ID
109            fwrite($fi,"【I      D】");
110            foreach ($info["ID"] as $k=>$v){
111                fwrite($fi,$v);
112            }
113            fwrite($fi,"rn");
114            //链接
115            fwrite($fi,"【链    接】rn");
116            foreach ($info["link"] as $value){
117                foreach ($value as $k=>$v){
118                    fwrite($fi, "  $v  ");
119                }
120                fwrite($fi,"rn");
121            }
122            fwrite($fi,"rn");
123            //评论
124            fwrite($fi,'【评    论】');
125            fwrite($fi,$info["gd"]["@numRaters"]);
126            fwrite($fi,"rn");
127            fwrite($fi,'【最高】');  fwrite($fi,$info["gd"]["@max"]);
128            fwrite($fi,"  【最低】");fwrite($fi,$info["gd"]["@min"]);
129            fwrite($fi,'  【平均】');fwrite($fi, $info["gd"]["@average"]);
130            fwrite($fi,"rn");
131}
132//在指定的文件夹中寻找电影并返回电影名
133function find_dir($dir)
134{
135    if(is_dir($dir)){                           //判断所给路径名是否为目录
136        if($fdir=opendir($dir)){                //打开目录
137            while(false!==($file=readdir($fdir))){              //遍历目录中的文件
138                if ($file != "." && $file != "..")
139                    $files[]=$file;                 //将寻找到的文件放入数组并返回
140            }
141            closedir($fdir);
142            print_r($file);
143            return $files;
144        }
145    }
146}
147//将生成的txt文件放在指定的目录下
148function write_to_dir($dir,$moviename)
149{
150    if (is_dir($dir)){                                                      //判断所给路径名是否为目录
151        if($fdir=opendir($dir)){                                            //打开目录
152            while(false!==($file=readdir($fdir))){                          //遍历目录中的文件
153                if ($file != '.' && $file != '..') {
154                    $dirname=$dir.'\'.$file;
155                    if (is_dir($dirname)&&$file==$moviename) {                  //寻找指定的文件夹
156                        $filename=$dirname.'\'.$moviename.'.txt';
157                        //if(!file_exists($filename)){
158                            if($fi=fopen($filename,'w+')){                       //打开指定文件,若不存在,建立文件并返回
159                                closedir($fdir);
160                                return $fi;
161                            }else
162                                echo"Fail to Open file!";
163                        //}
164                    } else
165                            echo "Dir Not Exists!";
166                }
167            }
168        } else {
169                        echo 'Fail to Open!';
170                        return;
171        }
172    }   else {
173                echo 'Directory Not Exists!';
174                return;
175    }
176}
177$dir = 'E:film';
178if($movienames = @find_dir($dir)){                              //在指定目录中获得电影名,进行搜索
179    foreach ($movienames as $key=>$moviename){
180        if($link_info = search_link($moviename)){
181            //搜索影片名
182            if ($fi=write_to_dir($dir,$moviename)){                 //写入指定文件夹
183                fwrite($fi,"<Resources>rn");
184                fwrite($fi,'<MesageBody>');
185                fwrite($fi,"rnrnrn".'※※※※※※※※※※※※※※※※※※※※※ 影片信息 ※※※※※※※※※※※※※※※※※※※※※'."rn");
186                foreach ($link_info as $k){
187                    $info=get_info($k);                             //获取链接信息
188                    write_info($fi,$info);                          //在txt文件中写入信息
189                    fwrite($fi,"*************************************************************************************************rn");
190                }
191                fclose($fi);//写入完成,关闭文件
192            }
193        } else
194            echo "Empty!";
195    }
196}   else
197        echo "Movies Not Exists!";
198?>