2011-08-29 125 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 ......似乎 –