2011-01-21 2 views
0

xkcd 만화를 다운로드하기 위해 PHP 스크립트를 작성하면서 특정 만화책을 얻으 려 할 때 오류가 발생했습니다 (최신 버전과 반대). 특히, 다음 URL에서 file_get_contents를 가리키는 : php의 file_get_contents가 xkcd에서 json 파일을 잘못 검색하는 이유는 무엇입니까?

xkcd.com/$COMIC_NUM/info.0.json

는 알수없는 XHTML의 xkcd.com에 만화의 페이지 버전이 아닌 JSON 파일을 검색. 그러나 브라우저에서 정확히 동일한 URL을 요청하면 올바른 JSON 파일이 다운로드됩니다. 왜 이런 일이 일어나고 있는지 모르겠지만 요청 헤더가 전송되는 것과 관련이 있다고 생각됩니다.

도와주세요! : 그것은 개인적으로 나를 위해 file_get_contents와 함께 완벽하게 작동 I 하다니 S

+1

어쩌면 당신은 시도해야 [컬 (http://php.net/manual/en/book.curl.php) – BoltClock

+0

그리고 요청 헤더는? – Gordon

+3

이것을 확인할 수 없습니다. ''JSON을 제공합니다. – Oswald

답변

2

, 당신은 다음과 같이 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); 
?> 
0

작품 벌금 나를. 어쩌면 $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"} 
관련 문제