0

好的,我有这个脚本应该从http://www.omdbapi.com/获取结果

<?php
$doc = new DOMDocument;
$filePath = 'data.txt';

    if(file_exists($filePath)) {
        $file = fopen($filePath,'r');
        while(!feof($file)) {
            $name = fgets($file);
            echo $name;
            $doc->loadhtmlfile('http://www.omdbapi.com/?i=&t=' . $name . '&r=JSON&plot=full');

            $body = $doc->getElementsByTagName('body');
            if ( $body && 0<$body->length ) {
                $body = $body->item(0);
                echo $doc->savehtml($body);
                }
                sleep(1);
        }
        fclose($file);
    } 
    else {
        echo "FNF!";
    }
?>

这是脚本,这是我正在使用的文件

Planet Earth
Game of Thrones
Breaking Bad
The Wire
Life
Sherlock
Arrested Development
Clanul Soprano
Firefly
Dexter
Avatar: The Last Airbender
Twin Peaks
Freaks and Geeks
Monty Python's Flying Circus
Roma
The Twilight Zone
Oz
Seinfeld
Deadwood
Desu n&#xF4;to
House of Cards
South Park
Fawlty Towers
Batman
Prietenii tai
Leyla ile Mecnun
Cowboy Bebop
Suits
It's Always Sunny in Philadelphia
Blackadder Goes Forth
Six Feet Under
Downton Abbey
The Office
The Walking Dead
Archer
Dragon Ball Z
Black-Adder II
Familia Simpson
Top Gear
Boardwalk Empire
The X Files
Battlestar Galactica
Justified
Black Adder the Third
Futurama
Doctor Who
Spartacus: Blood and Sand
Mad Men
Dragon Ball Z
Spaced

除了最后一个标题之外,我得到“找不到电影”(我最后放在文件中的标题无关紧要)。你能告诉我为什么会这样吗?

4

1 回答 1

2

尝试:

$name = trim(fgets($file));

当您调用fgets它时,它会返回最后带有换行符的行。最后一行没有换行符。

于 2013-07-14T06:56:29.327 回答