Getting xml file through command-line php script
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_seto开发者_开发百科pt($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?
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.
精彩评论