search
Homephp教程php手册php采集天气预报2段代码

现在天气预报网站都提供了解api来调用了,我们可以直接调用并显示在自己的网站上,下面我来给大家分享几段调用天气预报信息的php实例程序吧.

js调用天气预报方法,中国气象台,代码如下:

<iframe src="http://m.weather.com.cn/m/pn12/weather.htm " width="245" height="110"   
	 marginwidth="0" marginheight="0" hspace="0" vspace="0" frameborder="0"   
	 scrolling="no"></iframe>

上面是使用了框架方法,不好改自己的格式风格,最简单办法,代码如下:

<?php 
	header("content-type:text/html;charset=utf-8"); 
	$weather = file_get_contents("http://www.weather.com.cn/data/sk/101280601.html"); 
	echo $weather; 
	

html代码如下:

<html> 
	<head> 
	<meta http-equiv="Content-Type" content="text/html;charset=gbk" /> 
	<style type="text/css"> 
	.all span {font:bold 30px/50px "宋体";color:red;} 
	</style> 
	<title>天气预报</title> 
	</head> 
	<body> 
	<div class="all"> 
	这里是:<span class="place">城市</span>, 
	气温是<span class="temp">气温</span>, 
	风向:<span class="wind">风向</span>, 
	风力:<span class="windPower">风力</span> 
	</div> 
	<script type="text/javascript" src="http://127.0.0.1/jquery.js"></script> 
	<script type="text/javascript"> 
	$(function () { 
	$.ajax({ 
	//请求的地址 
	url : "http://127.0.0.1/weather.php", 
	//请求成功后执行的函数 
	success : function (data) { 
	//用eval()解析返回来的数据,将字符串转成JSON格式 
	var oD = eval((+data+)); 
	//用jquery-1.8.2获取元素 
	var $place = $(".place"), 
	$temp = $(".temp"), 
	$wind = $(".wind"), 
	$windPower = $(".windPower"); 
	//将返回来的数据放到相应的位置 
	$place.html(oD["weatherinfo"]["city"]); 
	$temp.html(oD["weatherinfo"]["temp"] + "&deg;"); 
	$wind.html(oD["weatherinfo"]["WD"]); 
	$windPower.html(oD["weatherinfo"]["WS"]); 
	} 
	}); 
	}) 
	</script> 
	</body> 
	</html>

上面是指定城市了,我们可以在自己网站选择城市,例子代码如下:

<?php 
	/** 
	* php 天气预报代码 
	* by www.phprm.com 
	*/ 
	    if(!isset($_GET[&#39;q&#39;])){ 
	        //根据IP查询所在地 
	        $p = file_get_contents("http://www.jbxue.com/ip/?q={$_SERVER[&#39;HTTP_X_FORWARDED_FOR&#39;]}"); 
	    } 
	    else{ 
	        $p = $_GET[&#39;q&#39;]; 
	    } 
	    $k = 0; 
	    $encoding = mb_detect_encoding()($p); 
	    if($encoding != "UTF-8"){ 
	        $p = mb_convert_encoding($p,"utf-8","gbk"); 
	    } 
	    $p_arr = array( 
	       "01" => "北京", 
	      "02" => "上海", 
	      "03" => "天津", 
	      "04" => "重庆", 
	      "05" => "黑龙江", 
	      "06" => "吉林", 
	      "07" => "辽宁", 
	      "08" => "内蒙古", 
	      "09" => "河北", 
	      "10" => "山西", 
	      "11" => "陕西", 
	      "12" => "山东", 
	      "13" => "新疆", 
	      "14" => "西藏", 
	      "15" => "青海", 
	      "16" => "甘肃", 
	      "17" => "宁夏", 
	      "18" => "河南", 
	      "19" => "江苏", 
	      "20" => "湖北", 
	      "21" => "浙江", 
	      "22" => "安徽", 
	      "23" => "福建", 
	      "24" => "江西", 
	      "25" => "湖南", 
	      "26" => "贵州", 
	      "27" => "四川", 
	      "28" => "广东", 
	      "29" => "云南", 
	      "30" => "广西", 
	      "31" => "海南", 
	      "32" => "香港", 
	      "33" => "澳门", 
	      "34" => "台湾" 
	    ); 
	 
	    function find(&$item,$key,$data){ 
	        global $k; 
	        if(preg_match("/$item/u",$data)){ 
	           $k = $key; 
	        } 
	    } 
	 
	    function get_data_arr($key){ 
	        if(!file_exists("./data/city{$key}.xml")){ 
	            $c =file_get_contents( "http://m.weather.com.cn/data5/city{$key}.xml" );   
	            file_put_contents("./data/city{$key}.xml",$c); 
	        } 
	        else{ 
	            $c = file_get_contents("./data/city{$key}.xml"); 
	        } 
	       $arr = explode(",",$c); 
	       foreach($arr as $v){ 
	           $data = explode("|",$v); 
	           $ret[$data[0]] = $data[1]; 
	       } 
	       return $ret; 
	    } 
	 
	    array_walk($p_arr,&#39;find&#39;,$p); 
	    array_walk(get_data_arr($k),&#39;find&#39;,$p); 
	    array_walk(get_data_arr($k),&#39;find&#39;,$p); 
	    $ccode = get_data_arr($k); 
	    echo file_get_contents("http://m.weather.com.cn/data/{$ccode[$k]}.html"); 
	

还有很多像可以调用更详细的天气信息的我们这里就不介绍了,大家可到天气网站去看他们提供的api接口.


文章网址:

随意转载^^但请附上教程地址。

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.