在編寫一個PHP腳本來下載xkcd漫畫時,我在嘗試獲取特定漫畫(而不是最新漫畫)時發生錯誤。具體來說,指向以下網址的file_get_contents:爲什麼php的file_get_contents錯誤地從xkcd中檢索json文件?
莫名其妙地檢索到的漫畫的頁面上xkcd.com的XHTML版本,而不是一個JSON文件。 但是,如果我在瀏覽器中請求完全相同的網址,則會下載正確的JSON文件。 我不知道爲什麼會發生這種情況,但我懷疑它與要發送的請求標頭有關。
請幫忙! :S
在編寫一個PHP腳本來下載xkcd漫畫時,我在嘗試獲取特定漫畫(而不是最新漫畫)時發生錯誤。具體來說,指向以下網址的file_get_contents:爲什麼php的file_get_contents錯誤地從xkcd中檢索json文件?
莫名其妙地檢索到的漫畫的頁面上xkcd.com的XHTML版本,而不是一個JSON文件。 但是,如果我在瀏覽器中請求完全相同的網址,則會下載正確的JSON文件。 我不知道爲什麼會發生這種情況,但我懷疑它與要發送的請求標頭有關。
請幫忙! :S
雖然我親自跟的file_get_contents對我的作品完美,你可以嘗試使用cURL如下(如果有的話可用),因爲這將是一個更強大的解決方案:
<?php
$COMIC_NUM = 849;
$curlSession = curl_init();
curl_setopt($curlSession, CURLOPT_URL, '"http://xkcd.com/'.$COMIC_NUM.'/info.0.json');
curl_setopt($curlSession, CURLOPT_BINARYTRANSFER, true);
curl_setopt($curlSession, CURLOPT_RETURNTRANSFER, true);
$jsonData = curl_exec($curlSession);
curl_close($curlSession);
echo($jsonData);
?>
工作正常我。可能$COMIC_NUM
設置不正確?
php > echo file_get_contents('http://xkcd.com/847/info.0.json');
{"img": "http://imgs.xkcd.com/comics/stingray_nebula.png", "title": "Stingray Nebula", "month": "1", "num": 847, "link": "", "year": "2011", "news": "", "safe_title": "Stingray Nebula", "transcript": "[[Two white figures are silhouetted against a dark sky. They're sitting on top of a grassy hill.]]\nPerson: I know things are tough right now. When I was going through some difficult times as a kid, I would go up on the roof and look through my telescope.\n\nPerson: One day I found a tiny star in Ara that seemed friendly.\nPerson: There were millions like it, but I decided that this one was mine.\n\nPerson: When things got bad, I'd go find that star, and think of my favorite Tolkien quote. It's from Sam's time in Mordor.\n\n((The next panel is diagonally downward to the right of the previous. The upper left corner overlaps.))\n[[A star is above the highest peak in a chain of mountains.]]\n\"There, peeping among the cloud-wrack above a dark tor high up in the mountains, Sam saw a white star twinkle for a while. The beauty of it smote his heart, as he looked up out of the forsaken land, and hope returned to him. For like a shaft, clear and cold, the thought pierced him that in the end the shadow was only a small and passing thing: There was light and high beauty forever beyond its reach.\"\n- The Return of the King\n\nCompanion: That's comforting!\nPerson: It was rather undercut in 1987, when the light from my star's explosion reached Earth. The debris forms the Stingray Nebula.\n\nCompanion: There's probably a lesson there.\nPerson: \"Never trust an unstable asymptotic giant branch star. Stick with main sequences and dwarfs.\"\nCompanion: I'll, uh, keep that in mind.\n\n{{Title text: E\u00c3\u00a4rendil will patrol the walls of night only until the sun reaches red giant stage, engulfing the Morning Star on his brow. Light and high beauty are passing things as well.}}", "alt": "E\u00c3\u00a4rendil will patrol the walls of night only until the sun reaches red giant stage, engulfing the Morning Star on his brow. Light and high beauty are passing things as well.", "day": "14"}
也許你應該試試[cURL](http://php.net/manual/en/book.curl.php) – BoltClock 2011-01-21 16:50:17