我正在使用 php 和 cURL,从 noaa 的网站上提取海洋预报。该脚本运行良好,直到预测区域离岸超过 60 英里。当预测离岸超过 60 英里时,页面会重定向。我的问题是如何获取新网址并仅在新页面上打印我想要的信息。这是我到目前为止所拥有的。它显然不起作用,但我认为它可能有助于展示我想要完成的事情。
<?php
$url = "http://forecast.weather.gov/MapClick.php?lat=$a1[d_lat]&lon=-$a1[d_lon]&unit=0&lg=english&FcstType=text&TextType=1";
//unique text to determine start goes here
$start = "</table><table width=";
//insert end text here
$end = "which includes this point<br></td></tr><tr>";
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url );
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec ($ch) or die ("Couldn't connect to $url.");
curl_close ($ch);
$startposition = strpos($result,$start);
if($startposition > 0){
$endposition = strpos($result,$end, $startposition);
//add enough chars to include the tag
$endposition += strlen($end);
$length = $endposition-$startposition;
$result = substr($result,$startposition,$length);
echo $result;
}else
//get the new url from headers? This is where I'm Lost...
$url = "";
//unique text to determine start on redirected page
$start = "</table><table width=";
//insert end text here
$end = "which includes this point<br></td></tr><tr>";
$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $url );
curl_setopt ($ch, CURLOPT_HEADER, 0);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec ($ch) or die ("Couldn't connect to $url.");
curl_close ($ch);
$startposition = strpos($result,$start);
if($startposition > 0){
$endposition = strpos($result,$end, $startposition);
//add enough chars to include the tag
$endposition += strlen($end);
$length = $endposition-$startposition;
$result = substr($result,$startposition,$length);
echo $result;
}else
echo "Sorry Forecast Unavailable";
?>