2013-03-09 50 views
0

我製作了一個腳本,它爲XML中的電影生成一個IMDB API鏈接。將頁面另存爲XML

一旦這個鏈接生成,它將保存到一個XML文件及其內容。唯一的問題是內容不能保存。

鏈接生成:
http://imdbapi.org/?title=One+Piece&type=xml&plot=simple&mt=none&episode=0&aka=simple&release=simple

PHP腳本:

$url="http://imdbapi.org/?title=One+Piece&type=xml&plot=simple&mt=none&episode=0&aka=simple&release=simple"; 

    $curl = curl_init(); 
    $data = fopen("text.xml", "w"); 
    curl_setopt ($curl, CURLOPT_URL, $url); 
    curl_setopt($curl, CURLOPT_FILE, $data); 
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE); 
    curl_exec ($curl); 

    if (!$data) { 
    echo "No"; 
} else { 
    $contents = curl_exec($curl); 
    fwrite($data, $contents); 
} 

curl_close($curl); 
fclose($data); 
+0

一般來說這個作品,但是你可能想[加大錯誤報告,因此PHP告訴你有關通知,警告和錯誤]( http://stackoverflow.com/a/14504459/367456)。 – hakre 2013-03-09 07:21:06

+0

另一個愚蠢的錯字問題... – 2013-03-09 07:21:16

回答

0

而不是使用的file_get_contents的,你可以使用捲曲

$ch = curl_init('http://imdbapi.org/?title=One+Piece&type=xml&plot=simple&mt=none&episode=0&aka=simple&release=simple'); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
$response = curl_exec($ch); 
curl_close($ch); 

現在是$回覆應包含您的XML。你可以這樣做

file_put_contents('filename.xml', $response); 

確保filename.xml中是可寫的

+0

但這不是什麼問。 – hakre 2013-03-09 07:27:51

+0

@hakre:謝謝。我試着改進它 – goFrendiAsgard 2013-03-09 07:32:26

+0

我已更新原始回覆。現在的問題是,它會寫入一個文本文件,但不是一個xml – Ryahn 2013-03-09 07:49:44