首頁 >CMS教程 >&#&按 >wordpress怎麼取得新浪天氣

wordpress怎麼取得新浪天氣

藏色散人
藏色散人原創
2020-01-10 09:59:062677瀏覽

wordpress怎麼取得新浪天氣

wordpress怎麼取得新浪天氣?

wordpress取得新浪天氣打造wordpress側邊欄天氣(wordpress外掛)

#推薦:《wordpress教學

WordPress取得新浪天氣,打造部落格側邊欄天氣,下面的php程式碼直接放到側邊欄就行了,當然其它地方也可以

  

取得IP有兩種方法,複雜的被我註解掉了,如果你想使用複雜的,去掉註釋,再把簡單的註解掉就可以了。

程式碼如下:

<?php
// 复杂获取本地ip地址
/* if (getenv(&#39;HTTP_CLIENT_IP&#39;) && strcasecmp(getenv(&#39;HTTP_CLIENT_IP&#39;), &#39;unknown&#39;)) {
$SA_IP = getenv(&#39;HTTP_CLIENT_IP&#39;);
} elseif (getenv(&#39;HTTP_X_FORWARDED_FOR&#39;) && strcasecmp(getenv(&#39;HTTP_X_FORWARDED_FOR&#39;), &#39;unknown&#39;)) {
$SA_IP = getenv(&#39;HTTP_X_FORWARDED_FOR&#39;);
} elseif (getenv(&#39;REMOTE_ADDR&#39;) && strcasecmp(getenv(&#39;REMOTE_ADDR&#39;), &#39;unknown&#39;)) {
$SA_IP = getenv(&#39;REMOTE_ADDR&#39;);
} elseif (isset($_SERVER[&#39;REMOTE_ADDR&#39;]) && $_SERVER[&#39;REMOTE_ADDR&#39;] && strcasecmp($_SERVER[&#39;REMOTE_ADDR&#39;], &#39;unknown&#39;)) {
$SA_IP = $_SERVER[&#39;REMOTE_ADDR&#39;];
} */
$SA_IP=$_SERVER[&#39;REMOTE_ADDR&#39;];//简单获取本地ip地址
//定义一个函数根据ip获取城市名,使用新浪的天气预报
function getIPLoc_sina($queryIP){
$url = &#39;http://int.dpool.sina.com.cn/iplookup/iplookup.php?format=json&ip=&#39; . $queryIP;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_ENCODING, &#39;utf8&#39;);
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 &#39;<aside><div><div>&#39; .$city .&#39; / &#39;.$date.&#39;</div><div><span>&#39;. $tmp1 .&#39;~&#39;. $tmp2 .&#39;<sup>℃</sup></span><span>&#39;.$stat1 .&#39;</span>
<span><p>空气质量:&#39;.$pollution_l .&#39;</p><p>穿衣建议:&#39;. $chy_shuoming .&#39;</p></span></div></div></div></aside>&#39;;
?>

CSS樣式

.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}

以上是wordpress怎麼取得新浪天氣的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn