How to get Sina weather in WordPress
How to get Sina weather in wordpress?
wordpress Get Sina weather to create wordpress sidebar weather (wordpress plug-in)
Recommendation: "wordpress tutorial"
WordPress gets Sina weather and creates a blog sidebar weather. Just put the following php code directly into the sidebar. Of course, it can also be done in other places.
There are two ways to get IP. I have commented out the complex ones. If you want to use the complex ones, just remove the comments and then comment out the simple ones.
The code is as follows:
<?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 style
.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}
The above is the detailed content of How to get Sina weather in WordPress. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











There are four ways to adjust the WordPress article list: use theme options, use plugins (such as Post Types Order, WP Post List, Boxy Stuff), use code (add settings in the functions.php file), or modify the WordPress database directly.

To build a website using WordPress hosting, you need to: select a reliable hosting provider. Buy a domain name. Set up a WordPress hosting account. Select a topic. Add pages and articles. Install the plug-in. Customize your website. Publish your website.

WordPress IP blocking plugin selection is crucial. The following types can be considered: based on .htaccess: efficient, but complex operation; database operation: flexible, but low efficiency; firewall: high security performance, but complex configuration; self-written: highest control, but requires more technical level.

A step-by-step guide to replacing a header image of WordPress: Log in to the WordPress dashboard and navigate to Appearance >Theme. Select the topic you want to edit and click Customize. Open the Theme Options panel and look for the Site Header or Header Image options. Click the Select Image button and upload a new head image. Crop the image and click Save and Crop. Click the Save and Publish button to update the changes.

WordPress editing dates can be canceled in three ways: 1. Install the Enable Post Date Disable plug-in; 2. Add code in the functions.php file; 3. Manually edit the post_modified column in the wp_posts table.

The steps to create a custom header in WordPress are as follows: Edit the theme file "header.php". Add your website name and description. Create a navigation menu. Add a search bar. Save changes and view your custom header.

Importing WordPress source code requires the following steps: Create a sub-theme for theme modification. Import the source code and overwrite the files in the sub-topic. Activate the sub-theme to make it effective. Test the changes to make sure everything works.

You can view the WordPress front-end by logging into the dashboard and switching to the View Sites tab; automate the viewing process with a headless browser; installing the WordPress plugin to preview the front-end within the dashboard; viewing the front-end via a local URL (if WordPress is set locally).
