2011-08-29 4 views
0

명령 줄 PHP 스크립트를 통해 XML 파일의 내용을 가져 오는 방법은 무엇입니까? IE 브라우저를 통해이 링크에 액세스하면 XML 파일 인 http://alerts.weather.gov/cap/ma.php?x=0을 얻을 수 있습니다. c:\path\php.exe get_advisory_upd.php과 함께 명령 줄을 통해 파일을 가져 오려고하면 스크립트에 host did not respond in allowed time 오류가 표시됩니다. 이것은 보안 문제로 보입니다. 지정된 간격으로 해당 XML을 가져 오려면 예약 된 작업이 있어야합니다. 어떻게해야합니까? file_get_contents()가 동일한 오류를 반환했습니다. simplexml_load_file()은 오류를 표시하지 않았지만 xml 파일을 가져 오지 않았습니다.명령 줄 PHP 스크립트를 통해 xml 파일 가져 오기

PHP 스크립트 get_advisory_upd.php :

<?php 

ini_set('display_errors', 1); 
error_reporting(E_ALL); 

$file = 'atom-advisory-MAZ015_UPD.txt'; 
$newfile = 'atom-advisory-MAZ015.txt.bak'; 

if (!copy($file, $newfile)) { 
    echo "Failed to copy $file...\n"; 
} 

/* $contents = file_get_contents('http://alerts.weather.gov/cap/ma.php?x=0'); */ 

// Use cURL to get the RSS feed into a PHP string variable. 
$ref_url = "http://192.x.x.x/weather/get_advisory_upd.php"; 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL, 'http://alerts.weather.gov/cap/ma.php?x=0'); 
curl_setopt($ch, CURLOPT_HEADER, false); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_REFERER, $ref_url); 
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 1000); 
$contents = curl_exec($ch); 
echo 'Curl error: '. curl_error($ch); 
curl_close($ch); 

/* $contents = simplexml_load_file('http://alerts.weather.gov/cap/ma.php?x=0'); 

echo "contents \n".$contents; */ 

file_put_contents($file, $contents); 

?> 

UPDATE

내가 인트라넷에서이 스크립트를 실행하고 있습니다. y_a_v_a 제안에 따라 원격 호스트에게 내 URL을 알리기 위해 CURLOPT_REFERER 옵션을 지정했습니다. 그걸로

$ref_url = "http://192.x.x.x/weather/get_advisory_upd.php"; 
curl_setopt($ch, CURLOPT_REFERER, $ref_url); 

URL을 지정하는 다른 방법이 있습니까?

답변

1

정상적인 CURLOPT_REFERER를 설정하고 CURLOPT_CONNECTTIMEOUT을 0으로 설정하고 스크립트를 실행하십시오. 컬 동작을 닫은 후에

$curl_error = curl_error($ch); 

을 입력하고 $ curl_error를 덤프하면 어떻게되는지 확인하십시오.

+0

오류 : 'Curl 오류 : 호스트 콘텐트에 연결할 수 없습니다.' – user823527

+0

인트라넷에서이 스크립트를 실행 중임을 언급해야합니다. CURLOPT_REFERER에 URL을 지정하기 위해,이'curl_setopt ($ ch, CURLOPT_REFERER, $ ref_url);와 같은 호스트 IP 주소를 $ ref_url을'$ ref_url = "http://192.xxx/weather/get_advisory_upd로 설정하여 제공합니다. PHP "; – user823527

+0

서버에서 URL을 핑 (ping)하여 액세스 할 수 있는지 확인 했습니까? 웃긴 점은 호스트를 ping 할 수는 없지만 커맨드 라인에서'curl http : //alerts.weather.gov/cap/ma.php? x = 0'을 실행하면 xml을 얻을 수 있다는 것입니다 ... –

관련 문제