<!doctype html> <html> <head> <meta charset="utf-8"> <title>api</title> <script src="./jquery-2.1.1.min.js"></script> </head> <style> ul{ width: 380px; border: inset; border-color: #6c65ff; } li{ list-style-type:none } </style> <body> <center> <form name="form"> <b style="color: #ff464f">查询天气:</b><input type="text" name="price" id="city" style="border-bottom-style: inset;height: 20px; width: 100px;"> <input type="button" class="btn" value="查询"/> </form> <table border="5" class="table_city" style="display: none"> <th>当前城市</th> <th>当前日期</th> <th>星期</th> <th>温度</th> <th>天气</th> <th>风向</th> <th>风力</th> <th>最高温度</th> <th>最低温度</th> <th>天气状况</th> <tbody id="citySearch"></tbody> </table> </center> <div class="tel_search"> <b style="color: #ff464f">查询手机归属地:</b> <input type="text" name="price" id="tel" style="border-bottom-style: inset;height: 20px;width: 100px;"> <input type="button" class="btn_tel" value="查询"/> <div id="telSearch"></div> </div> </body> </html><script type="text/javascript"> $(function(){ $(".btn").click(function() { $(".table_city").show(); //var j = $("form").serializeArray();//序列化name/value var city=$("#city").val(); $.ajax({ type: "GET", async: false, url:"http://api.k780.com:88/?app=weather.future&weaid="+city+"&&appkey=21376&sign=241bf4e35fe1b54f13fee939124dec41&format=json&&jsoncallback=data", dataType: "jsonp", jsonp: "callback",//传递给请求处理程序或页面的,用以获得jsonp回调函数名的参数名(一般默认为:callback) jsonpCallback:"data",//自定义的jsonp回调函数名称,默认为jQuery自动生成的随机函数名,也可以写”?”,jQuery会自动为你处理数据 success: function(data){ alert(data); var str=""; for(i in data.result){ str+="<tr align='center'>"; str+="<td>"+data.result[i].citynm+"</td>"; str+="<td>"+data.result[i].days+"</td>"; str+="<td>"+data.result[i].week+"</td>"; str+="<td>"+data.result[i].temperature+"</td>"; str+="<td>"+data.result[i].weather+"</td>"; str+="<td>"+data.result[i].wind+"</td>"; str+="<td>"+data.result[i].winp+"</td>"; str+="<td>"+data.result[i].temp_high+"</td>"; str+="<td>"+data.result[i].temp_low+"</td>"; str+="<td><img src="+data.result[i].weather_icon+"></td>"; str+="</tr>"; } $("#citySearch").html(str); }, error: function(){ alert("查询失败"); } }); //… });}); </script> 第二种 php页面查询天气并发送邮件<?php header("content-type:text/html;charset=utf-8"); $city=$_GET['city']; $apiurl="http://api.k780.com:88/?app=weather.future&weaid=".$city."&&appkey=*****&sign=241bf4e35fe1b54f13fee939124****&format=json"; $content=@file_get_contents($apiurl); $result = json_decode($content,true); $con=$result['result'][0]['citynm'].$result['result'][0]['days'].$result['result'][0]['week'].$result['result'][0]['temperature'].$result['result'][0]['weather']."<br>"; //$con=$result['result'][1]['citynm'].$result['result'][1]['days'].$result['result'][1]['week'].$result['result'][1]['temperature'].$result['result'][1]['weather']."<br>"; //$str=array(); // for($i=0;$i<count($result['result']);$i++){ // $str.=$result['result'][0]['days']; // $str.=$result['result'][0]['days']; // $str.=$result['result'][0]['week']; // $str.=$result['result'][0]['temperature']; // $str.=$result['result'][0]['weather']; // } error_reporting(E_ALL); ini_set('display_errors', '1'); require './PHPMailer/class.phpmailer.php'; $mail= new PHPMailer(); /*服务器相关信息*/ $mail->IsSMTP(); //设置使用SMTP服务器发送 $mail->SMTPAuth = true; //开启SMTP认证 $mail->Encoding = "base64"; //编码方式 $mail->Host = "smtp.163.com"; //设置 SMTP 服务器,自己注册邮箱服务器地址 $mail->Username = '*************'; //发信人的邮箱名称 $mail->Password = '*******'; //发信人的邮箱密码 $mail->Port=25; /*内容信息*/ $mail->IsHTML(true); //指定邮件格式为:html 不加true默认为以text的方式进行解析 $mail->CharSet ="UTF-8"; //编码 $mail->From = '***********'; //发件人完整的邮箱名称 $mail->FromName = '远方的她'; //发信人署名 $mail->Subject = '天气情况'; //信的标题 $mail->MsgHTML("$con"); //发信主体内容 //$mail->AddAttachment("15.jpg"); //附件 /*发送邮件*/ $mail->AddAddress("943738180@qq.com"); //收件人地址 //使用send函数进行发送 if (!$mail->Send()) { echo "邮件发送失败."; echo "错误原因: " . $mail->ErrorInfo; exit; } echo $content;
调用接口查询天气
最新推荐文章于 2024-04-08 08:52:16 发布