[英]Getting xml file through command-line php script
如何通过命令行php脚本获取xml文件的内容? 如果我通过IE浏览器访问此链接,则可以获取XML文件: http://alerts.weather.gov/cap/ma.php?x=0
: 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_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);
?>
更新
我正在从Intranet运行此脚本。 根据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。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.