wordpress获取新浪天气打造wordpress侧边栏天气(wordpress插件)
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>获取IP有两种方法,复杂的被我注释掉了,如果你想使用复杂的,去掉注释,再把简单的注释掉就可以了。</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
</p>
<div style='margin: 3px auto 0px; padding: 0px 3px; outline: none; line-height: 21.6px; clear: both; border-width: 1px; border-style: solid; border-color: rgb(0, 153, 204); background: rgb(246, 251, 255); overflow: hidden; font-family: tahoma, arial, "Microsoft YaHei";'>
<div style="margin: 0px; padding: 0px; outline: none; float: right; line-height: 25.2px; font-size: 14px;">
<span style="line-height: 25.2px; cursor: pointer;"><u>复制代码</u></span>
</div>
<p>
代码如下:</p>
</div>
<div id="phpcode1" style='margin: 0px auto 3px; padding: 0px 3px; outline: none; line-height: 25.2px; font-size: 14px; clear: both; border-right: 1px solid rgb(0, 153, 204); background: rgb(221, 237, 251); overflow: hidden; border-left: 1px solid rgb(0, 153, 204); word-break: break-all; border-bottom: 1px solid rgb(0, 153, 204); word-wrap: break-word; font-family: tahoma, arial, "Microsoft YaHei";'>
<br>
<?php<br>
// 复杂获取本地ip地址<br>
/* if (getenv('HTTP_CLIENT_IP') && strcasecmp(getenv('HTTP_CLIENT_IP'), 'unknown')) {<br>
$SA_IP = getenv('HTTP_CLIENT_IP');<br>
} elseif (getenv('HTTP_X_FORWARDED_FOR') && strcasecmp(getenv('HTTP_X_FORWARDED_FOR'), 'unknown')) {<br>
$SA_IP = getenv('HTTP_X_FORWARDED_FOR');<br>
} elseif (getenv('REMOTE_ADDR') && strcasecmp(getenv('REMOTE_ADDR'), 'unknown')) {<br>
$SA_IP = getenv('REMOTE_ADDR');<br>
} elseif (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], 'unknown')) {<br>
$SA_IP = $_SERVER['REMOTE_ADDR'];<br>
} */<br>
$SA_IP=$_SERVER['REMOTE_ADDR'];//简单获取本地ip地址<br>
//定义一个函数根据ip获取城市名,使用新浪的天气预报<br>
function getIPLoc_sina($queryIP){<br>
$url = 'http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=' . $queryIP;<br>
$ch = curl_init($url);<br>
curl_setopt($ch, CURLOPT_ENCODING, 'utf8');<br>
curl_setopt($ch, CURLOPT_TIMEOUT, 10);<br>
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);<br>
$location = curl_exec($ch);<br>
$location = json_decode($location);<br>
curl_close($ch);<br>
$loc = "";<br>
if ($location === FALSE)<br>
return "";<br>
if (empty($location->desc)) {<br>
$loc = $location->city;<br>
$full_loc = $location->province . $location->city . $location->district . $location->isp;<br>
} else {<br>
$loc = $location->desc;<br>
}<br>
return $loc;<br>
}<br>
$city = getIPLoc_sina("$SA_IP");</p> <p>$citycode = mb_convert_encoding($city, "gb2312", "utf-8");</p> <p>$doc = new DOMDocument();<br>
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")) {<br>
echo "Get data failed!!\n";<br>
return;<br>
}</p> <p>$city = $doc->getElementsByTagName("city")->item(0)->nodeValue;<br>
$stat1 = $doc->getElementsByTagName("status1")->item(0)->nodeValue;<br>
$chy_shuoming = $doc->getElementsByTagName("chy_shuoming")->item(0)->nodeValue;<br>
$tmp1 = $doc->getElementsByTagName("temperature1")->item(0)->nodeValue;<br>
$tmp2 = $doc->getElementsByTagName("temperature2")->item(0)->nodeValue;<br>
$date = $doc->getElementsByTagName("savedate_weather")->item(0)->nodeValue;<br>
$pollution_l = $doc->getElementsByTagName("pollution_l")->item(0)->nodeValue;<br>
$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><br>
<span><p>空气质量:'.$pollution_l .'</p><p>穿衣建议:'. $chy_shuoming .'</p></span></div></div></div></aside>';<br>
?></div>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
</p>
<p style='margin: 0px; padding: 5px 0px; outline: none; font-size: 14px; line-height: 30px; font-family: tahoma, arial, "Microsoft YaHei";'>
CSS样式</p>
<blockquote>
<ol>
<li>
<span><span>.sina-weather-content{border:1px solid #D14836;font-size:12px;line-height:14px;position:relative} </span></span>
</li>
<li>
<span>.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} </span>
</li>
<li>
<span>.sina-weather-body{overflow:hidden;padding:45px 15px 15px} </span>
</li>
<li>
<span>.sina-weather-body p{margin-bottom:10px} </span>
</li>
<li>
<span>.sina-weather-tem{color:#000000;font-size:36px;font-weight:700;line-height:36px;padding-top:10px} </span>
</li>
<li>
<span>.sina-weather-text{color:#666666;display:block;margin-top:10px} </span>
</li>
<li>
<span>.stat-w{color:#555555;font-family:Microsoft Yahei;font-size:18px;font-style:italic;font-weight:600;line-height:10px} </span>
</li>
<li>
<span>.entry-meta{position:relative;font-size:12px;font-size:12px;margin-top:-15px;padding:10px 0;text-transform:uppercase} </span>
</li>
<li>
<span>.entry-meta a{color:#666} </span>
</li>
</ol>
</blockquote>
頁:
[1]