您好,登錄后才能下訂單哦!
這篇文章將為大家詳細(xì)講解有關(guān)wordpress獲取新浪天氣的方法,小編覺得挺實用的,因此分享給大家做個參考,希望大家閱讀完這篇文章后可以有所收獲。
wordpress怎么獲取新浪天氣?
wordpress獲取新浪天氣打造wordpress側(cè)邊欄天氣(wordpress插件)
WordPress獲取新浪天氣,打造博客側(cè)邊欄天氣,下面的php代碼直接放到邊欄就行了,當(dāng)然其它地方也可以
獲取IP有兩種方法,復(fù)雜的被我注釋掉了,如果你想使用復(fù)雜的,去掉注釋,再把簡單的注釋掉就可以了。
代碼如下:
<?php // 復(fù)雜獲取本地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地址 //定義一個函數(shù)根據(jù)ip獲取城市名,使用新浪的天氣預(yù)報 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>空氣質(zhì)量:'.$pollution_l .'</p><p>穿衣建議:'. $chy_shuoming .'</p></span></div></div></div></aside>'; ?>
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}
關(guān)于wordpress獲取新浪天氣的方法就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,可以學(xué)到更多知識。如果覺得文章不錯,可以把它分享出去讓更多的人看到。
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。