取得新浪天氣預報網頁的城市當日資訊.
共有兩份文件:
tianqi.html:利用下拉式選單跳到網頁取得天氣預報資訊,回傳資訊到浮動影格;
<script> <br/><!-- <br/>function go(){ <br/>var url2='http://php.weather.sina.com.cn/search.php?city=' <br/>window.status='搜尋中...'; //隱藏狀態列資訊 <br/>var a=document.form.city.selectedIndex; <br/>var city1=document.form.city.options[a].value; <br/>document.form.url.value =url2+city1; <br/>//window.location=url+city1; <br/>document.form.submit(); <br/>} <br/>--> <br/></script>
-->
-->
-->
-->
300" height="70" name="frame" src="get_tianqi.php" frameborder="1">
get_tianqi.php:取得天氣預報資訊
/* 此程式是從新浪網的天氣網頁http://php.weather.sina.com.cn/search.php?city=取得某個城市的天氣預報*/
ob_start(); //啟動輸出緩衝
function request_url($url,$method='POST') {
$url = parse_url($url); //解析url port,query等
if (!$url) return "couldn't parse url";
if (!isset($url['port'])) { $url['port'] = ""; }
if (!isset($url['query'])) { $url['query'] = ""; }
//連接伺服器
$fp = fsockopen($url['host'], $url['port '] ? $url['port'] : 80);
if (!$fp) return "不能連接".$url['host']."伺服器";
//發送請求
fputs($fp, sprintf("$method %s%s%s HTTP/1.0n", $url['path'], $url['query'] ? "?" : "", $url['query']));
fputs($fp, "Host: $url[host]n");
fputs($fp, "Content-type: application/x-www-form-urlencodedn");
fputs(Connection$fp, "Connection$ closenn");
//取得請求後回傳的內容
$line = fgets($fp,1024);
if (!eregi("^HTTP/1.. 200", $line)) return; = "";
while(!feof($fp)) {
$line = fgets($fp,1024);
$results .= $line
) }
//取得從其它網頁傳送過來的url(帶查詢字符串)
if (!$_REQUEST['url']) {
echo "請加一個url";
exit;
}else{
$
exit;
}else{
$ =$_REQUEST['url'];
}
$content=request_url($url); //取得請求後的網頁內容
$start=strpos($contentgin,"");
$len=$end-$start;
$b=substr ($content,$start,$len); //截取指定內容
//從上面的截取段取得城市今日天氣預報的截取段 🎜$sub_start=strpos($b,"
"); 🎜$sub_end=strpos($b,"
"); 🎜$len2=$sub_end-$sub_start; 🎜$c=substr($sub_end-$sub_start; 🎜$c=substr($sub ); 🎜$d=split("n",$c); //去掉換行符 🎜//print_r($d); 🎜$e=""; 🎜foreach ($d as $key=>$value) { 🎜$value=strip_tags($value); //移除html標記 🎜$e.=$值;
if ($key==2) {
$e.="
";
}
}
迴聲$e; >