通过命令行php脚本获取xml文件


Getting xml file through command-line php script

如何通过命令行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脚本get_advisory_up . 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);
?>

更新

我从内网运行这个脚本。正如y_a_v_a建议的那样,我指定了CURLOPT_REFERER选项来告诉远程主机我的url。我用

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

是否有不同的方式来指定URL?

设置一个相同的CURLOPT_REFERER并将CURLOPT_CONNECTTIMEOUT设置为零并运行脚本。通过添加

来验证发生了什么
$curl_error = curl_error($ch);
在关闭curl操作后转储$curl_error