2011-08-29 72 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

我正在從Intranet這個腳本。正如y_a_v_a建議我指定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設置爲零並運行腳本。驗證通過添加

$curl_error = curl_error($ch); 

並在關閉curl操作後轉儲$ curl_error。

+0

它得到的錯誤是:'捲曲錯誤:無法連接到hostcontents.' – 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

您是否從服務器ping通URL,查看它是否可訪問?有趣的是,我不能ping主機,但運行'捲曲的http://alerts.weather.gov/cap/ma.php X = 0'從我的命令行我得到XML ......似乎 –

相關問題