发新话题
打印

天气预报'插件'

天气预报'插件'

能够根据来访者的IP显示其所在城市的天气情况
从百度获取的天气信息
效果见我的博客
http://www.lineks.cn

需要curl库,或allow_url_open支持,mbstring库或iconv库支持,有什么问题和我联系 : )


修改方法
主程序index.php
复制内容到剪贴板
代码:
require_once PrintEot('index');
footer();
修改为
复制内容到剪贴板
代码:

include 'weather.php';
require_once PrintEot('index');
footer();
weather.php内容
复制内容到剪贴板
代码:
<?php
$ip=$_SERVER['REMOTE_ADDR'];
$localtion=convert(fopen_url("http://www.123cha.com/ip/?q=".$ip));
if(preg_match('<省(.+)市>',$localtion,$matches))
{
        $city=$matches[1];
}else if(preg_match('<(.+)市>',$localtion,$matches)){
        $city=$matches[1];
}else{
        $city='notmatch';
}

if($city!='notmatch')
{
        $cityurl="http://www.baidu.com/s?ie=gb2312&bs=%CE%E4%BA%BA%CC%EC%C6%F8&sr=&z=&cl=3&f=8&wd=".urlencode(convert($city,'utf-8','gb2312'))."+%CC%EC%C6%F8&ct=0";
    $str=fopen_url($cityurl);
        $pattern='!\<\/a\>\<\/font\>\<br\>(.+)\<\/td\>\<\/tr\>!';
        preg_match($pattern,$str,$matches);
    $matches=convert($matches[1]);
    $weatherinfo=preg_replace('<。>','。<br />',$matches);
}
function convert($out,$informat='gb2312',$outformat='utf-8') {
                if(function_exists('mb_convert_encoding'))
                {
                        return mb_convert_encoding($out,$outformat,$informat);
                }else if(function_exists('iconv')){
                        return iconv('gb2312',$outformat,$out);
                }
}

function fopen_url($url) {
        if (function_exists('allow_url_fopen')) {
                $file_content = file_get_contents($url);
        }elseif (ini_get('allow_url_fopen') && ($file = @fopen($url, 'rb'))){
                $i = 0;
                while (!feof($file) && $i++ < 1000) {
                        $file_content .= strtolower(fread($file, 4096));
                }
                fclose($file);
        } elseif (function_exists('curl_init')) {
                $curl_handle = curl_init();
                curl_setopt($curl_handle, CURLOPT_URL, $url);
                curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT,2);
                curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER,1);
                curl_setopt($curl_handle, CURLOPT_FAILONERROR,1);
                $file_content = curl_exec($curl_handle);               
                curl_close($curl_handle);
        } else {
                $file_content = '';
        }
        return $file_content;
}
?>
[ 本帖最后由 irlvirus 于 2008-6-8 23:03 编辑 ]
→www.Lineks.cn
→招友情链接
→我是米粒

TOP

寒。。。你用的是2.0的!
个人博客:liupeng.us
欢迎光临作客!(*^__^*) 嘻嘻……
论坛有事,请直接给我发论坛短信!

TOP

不是2.0
1.6的
直接include执行的话比较耗时
javascript或Ajax调用为佳
→www.Lineks.cn
→招友情链接
→我是米粒

TOP

发新话题