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

发布于 2024-12-01 23:39:19 字数 1628 浏览 1 评论 0原文

如何通过命令行php脚本获取xml文件的内容?如果我通过 IE 浏览器访问此链接,我可以获得 XML 文件:http://alerts.weather.gov/cap/ma.php?x=0。如果我尝试使用 c:\path\php.exe get_advisory_upd.php 通过命令行获取文件,脚本会显示错误 主机未在允许的时间内响应。这似乎是一个安全问题。我必须有一个计划任务来按指定的时间间隔获取该 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 选项来告诉远程主机我的网址。我通过

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

Is there a different way to specify the URL?来做到这一点?

How do I get the content of an xml file through a command line php script? If I access this link through the IE browser, I am able to get the XML file: http://alerts.weather.gov/cap/ma.php?x=0. If I try to get the file through command line with c:\path\php.exe get_advisory_upd.php, the script shows an error host did not respond in allowed time. This seems to be a security issue. I must have a scheduled task to get that xml at specified intervals. How do I do that? file_get_contents() returned that same error, simplexml_load_file() might not have shown any errors, but did not get the xml file.

PHP Script 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

I am running this script from an intranet. As y_a_v_a suggested I specified the CURLOPT_REFERER option to tell the remote host my url. I do that with

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

Is there a different way to specify the URL?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

偏爱自由 2024-12-08 23:39:19

设置合理的 CURLOPT_REFERER 并将 CURLOPT_CONNECTTIMEOUT 设置为零并运行脚本。 通过添加并转储 $curl_error 来验证会发生什么情况。

$curl_error = curl_error($ch);

关闭卷曲操作后,

Set a sane CURLOPT_REFERER and set CURLOPT_CONNECTTIMEOUT to zero and run your script. Verify what happens by adding

$curl_error = curl_error($ch);

and dump $curl_error after you closed the curl action.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文