2014-12-06 3 views
0

나는 PHP 프로그래밍의 초보자입니다. 나는 시도하고 지속적으로 아래의이 독특한 파일의 내용을 가져 오는 데 실패했습니다 :PHP가 원격 파일의 내용을 읽을 수 없습니다.

http://nghenhacvang.net/playplaylist/5730.xml

API get_file_contents를 사용하여. 사용자 에이전트를 설정하지 않고 작동하지 않습니다 file_get_contents() 보인다

Remote Address:23.226.231.225:80 
Request URL:http://nghenhacvang.net/playplaylist/5730.xml 
Request Method:GET 
Status Code:200 OK 
Request Headersview source 
Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 
Accept-Encoding:gzip, deflate, sdch 
Accept-Language:en-US,en;q=0.8 
Connection:keep-alive 
Cookie:PHPSESSID=2g8id3hevm0sqo3qv8ln8n04h2; __utma=107959197.1514810948.1417822471.1417840071.1417854776.3; __utmc=107959197; __utmz=107959197.1417822471.1.1.utmcsr=search.pch.com|utmccn=(referral)|utmcmd=referral|utmcct=/search 
Host:nghenhacvang.net 
User-Agent:Mozilla/5.0 (Windows NT 6.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.71 Safari/537.36 
Response Headersview source 
Cache-Control:no-store, no-cache, must-revalidate, post-check=0, pre-check=0 
Connection:Keep-Alive 
Content-Encoding:gzip 
Content-Length:660 
Content-Type:text/xml 
Date:Sat, 06 Dec 2014 10:32:55 GMT 
Expires:Thu, 19 Nov 1981 08:52:00 GMT 
Keep-Alive:timeout=15, max=100 
Pragma:no-cache 
Server:Apache/2.2.3 (CentOS) 
Set-Cookie:SID=0a2f9b6ce1d6280ca5fa74e118a2e1b6; expires=Thu, 01-Jan-1970 01:00:00 GMT 
Vary:Accept-Encoding,User-Agent 
X-Powered-By:PHP/5.1.6 
+0

실패 했습니까? 방법? PHP 코드를 여기에 게시하십시오. –

+0

Indra 저는 Ghost가 옵션/컨텍스트 부분을 제외하고 정확히 구현하고 빈 문자열을 돌려받습니다. – RC2

답변

0

:이 파일은 다음과 같은 정보를 사용하여 크롬이있다. 헤더 사용자 에이전트를 설정하기 위해 추가 스트림 컨텍스트를 추가하십시오. 또한 대안으로 curl을 사용할 수 있습니다

$url = 'http://nghenhacvang.net/playplaylist/5730.xml'; 
$options = array('http' => array('user_agent' => 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13')); 
$context = stream_context_create($options); 
$contents = file_get_contents($url, false, $context); 
$xml = simplexml_load_string($contents); 

echo '<pre>'; 
print_r($xml); 

Sample Output

:

$url = 'http://nghenhacvang.net/playplaylist/5730.xml'; 
$ch = curl_init($url); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13'); 
$contents = curl_exec($ch); 

$xml = simplexml_load_string($contents); 

echo '<pre>'; 
print_r($xml); 

보너스 :

foreach($xml->channel->item as $item) { 
    $description = (string) $item->description; 
    $title = (string) $item->title; 
    $jwplayer = $item->children('jwplayer', 'http://rss.jwpcdn.com/'); 
    $source = (string) $jwplayer->source->attributes()->file; 
    $image_url = (string) $jwplayer->image; 

    // presentation 
    echo " 
    Title: $title <br/> 
    Description: $title <br/> 
    Source: $source <br/> 
    Image URL: $image_url <br/> 
    <hr/> 
    "; 
} 

Sample Output

: 여기에 내용을 검색 한 후 몇 가지 간단한 사용법이다
+0

Ghost, 훌륭한 솔루션에 감사드립니다. 나는 사용자 에이전트 문제에 대해 생각하지 않았을 것이다. 수업은 배웠다. – RC2

+0

@ RC2 잘 브라우저에서 먼저 테스트하고 잘 액세스했습니다. 일반 파일 가져 오기가 실패하면 방금 그 차이를 발견했습니다. 기꺼이 도움이되었습니다. – Ghost

관련 문제