wordpress获取新浪天气打造wordpress侧边栏天气(wordpress插件)
获取IP有两种方法,复杂的被我注释掉了,如果你想使用复杂的,去掉注释,再把简单的注释掉就可以了。
<?php
// 复杂获取本地ip地址
/* if (getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {
$SA_IP = getenv('HTTP_CLIENT_IP');
} elseif (getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {
$SA_IP = getenv('HTTP_X_FORWARDED_FOR');
} elseif (getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {
$SA_IP = getenv('REMOTE_ADDR');
} elseif (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {
$SA_IP = $_SERVER['REMOTE_ADDR'];
} */
$SA_IP=$_SERVER['REMOTE_ADDR'];//简单获取本地ip地址
//定义一个函数根据ip获取城市名,使用新浪的天气预报
function getIPLoc_sina($queryIP){
$url = 'http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=' . $queryIP;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_ENCODING, 'utf8');
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$location = curl_exec($ch);
$location = json_decode($location);
curl_close($ch);
$loc = "";
if ($location === FALSE)
return "";
if (empty($location->desc)) {
$loc = $location->city;
$full_loc = $location->province . $location->city . $location->district . $location->isp;
} else {
$loc = $location->desc;
}
return $loc;
}
$city = getIPLoc_sina("$SA_IP");</p> <p>$citycode = mb_convert_encoding($city, "gb2312", "utf-8");</p> <p>$doc = new DOMDocument();
if (<a href="mailto:!@$doc->load("http://php.weather.sina.com.cn/xml.php?city">!@$doc->load("http://php.weather.sina.com.cn/xml.php?city</a>=" . $citycode . "&password=DJOYnieT8234jlsK&day=0")) {
echo "Get data failed!!\n";
return;
}</p> <p>$city = $doc->getElementsByTagName("city")->item(0)->nodeValue;
$stat1 = $doc->getElementsByTagName("status1")->item(0)->nodeValue;
$chy_shuoming = $doc->getElementsByTagName("chy_shuoming")->item(0)->nodeValue;
$tmp1 = $doc->getElementsByTagName("temperature1")->item(0)->nodeValue;
$tmp2 = $doc->getElementsByTagName("temperature2")->item(0)->nodeValue;
$date = $doc->getElementsByTagName("savedate_weather")->item(0)->nodeValue;
$pollution_l = $doc->getElementsByTagName("pollution_l")->item(0)->nodeValue;
$gm_s = $doc->getElementsByTagName("gm_s")->item(0)->nodeValue;</p> <p>echo '<aside><div><div>' .$city .' / '.$date.'</div><div><span>'. $tmp1 .'~'. $tmp2 .'<sup>℃</sup></span><span>'.$stat1 .'</span>
<span><p>空气质量:'.$pollution_l .'</p><p>穿衣建议:'. $chy_shuoming .'</p></span></div></div></div></aside>';
?>
CSS样式
[code]
.sina-weather-content{border:1px solid #D14836;font-size:12px;line-height:14px;position:relative}
.sina-weather-city{background:#D14836;color:#FFFFFF;font-size:14px;font-weight:600;left:-1px;padding:5px 15px;position:absolute;top:5px;z-index:2;display:inline}
.sina-weather-body{overflow:hidden;padding:45px 15px 15px}
.sina-weather-body p{margin-bottom:10px}
.sina-weather-tem{color:#000000;font-size:36px;font-weight:700;line-height:36px;padding-top:10px}
.sina-weather-text{color:#666666;display:block;margin-top:10px}
.stat-w{color:#555555;font-family:Microsoft Yahei;font-size:18px;font-style:italic;font-weight:600;line-height:10px}
.entry-meta{position:relative;font-size:12px;font-size:12px;margin-top:-15px;padding:10px 0;text-transform:uppercase}
.entry-meta a{color:#666}
[/code
栏 目:WordPress
本文标题:wordpress获取新浪天气打造wordpress侧边栏天气(wordpress插件)
本文地址:https://www.xiuzhanwang.com/a1/WordPress/12811.html
您可能感兴趣的文章
阅读排行
本栏相关
- 05-31wordpress修改后台登录地址教程
- 05-31WordPress自定义用户头像实现代码
- 01-11WordPress增加文章排序方式
- 01-11修改wordpress上传临时目录解决wordpre
- 01-11wordpress get
- 01-11自己做wordpress评论插件修改评论样式
- 01-11在wordpress文章末尾添加内容的简单方
- 01-11wordpress获取文章评论数过滤掉作者代
- 01-11wordpress dynamic
- 01-11不用wordpress插件显示文章浏览统计功
随机阅读
- 08-05dedecms(织梦)副栏目数量限制代码修改
- 04-02jquery与jsp,用jquery
- 01-11ajax实现页面的局部加载
- 01-10SublimeText编译C开发环境设置
- 01-10C#中split用法实例总结
- 08-05DEDE织梦data目录下的sessions文件夹有什
- 01-10使用C语言求解扑克牌的顺子及n个骰子
- 08-05织梦dedecms什么时候用栏目交叉功能?
- 01-10delphi制作wav文件的方法
- 01-11Mac OSX 打开原生自带读写NTFS功能(图文