cUrl和重定向

时间:2013-02-22 01:21:34

标签: php curl

我正在使用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";
?>    

0 个答案:

没有答案