Commit b796dce5 authored by 季圣华's avatar 季圣华
Browse files

No commit message

No commit message
parent c9e03495
.NET proxy version
written by Alessandro Benedetti
You need the open source Newtonsoft.Json to compile.
\ No newline at end of file
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WeatherProxy.aspx.cs" Inherits="MultiConsultCRM.Web.Resources.WeatherProxy" %>
\ No newline at end of file
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml;
using System.Collections.Generic;
using System.Net;
using System.IO;
namespace MultiConsultCRM.Web.Resources
{
public partial class WeatherProxy : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string _location = Request.QueryString["location"];
string _metric = Request.QueryString["metric"];
//string _url = string.Format("http://rainmeter.accu-weather.com/widget/rainmeter/weather-data.asp?location={0}&metric={1}", _location, _metric);
string _url = string.Format("http://wwwa.accuweather.com/adcbin/forecastfox/weather_data.asp?location={0}&metric={1}", _location, _metric);
string _xml = DownloadWebPage(_url);
XmlDocument _xmlDocument = new XmlDocument();
_xmlDocument.LoadXml(_xml);
XmlNamespaceManager _mgr = new XmlNamespaceManager(_xmlDocument.NameTable);
_mgr.AddNamespace("pf", _xmlDocument.DocumentElement.NamespaceURI);
Weather _weather = new Weather();
_weather.city =
_xmlDocument.SelectSingleNode("/pf:adc_database/pf:local/pf:city", _mgr).InnerText;
_weather.curr_temp = Convert.ToInt32(
_xmlDocument.SelectSingleNode("/pf:adc_database/pf:currentconditions/pf:temperature", _mgr).InnerText);
_weather.curr_text =
_xmlDocument.SelectSingleNode("/pf:adc_database/pf:currentconditions/pf:weathertext", _mgr).InnerText;
_weather.curr_icon = Convert.ToInt32(
_xmlDocument.SelectSingleNode("/pf:adc_database/pf:currentconditions/pf:weathericon", _mgr).InnerText);
XmlNodeList _xmlNodeList = _xmlDocument.SelectNodes("/pf:adc_database/pf:forecast/pf:day", _mgr);
int _day = _xmlNodeList.Count;
int i = 0;
foreach (XmlNode _dayItem in _xmlNodeList)
{
Forecast _forecast = new Forecast();
_forecast.day_date = _dayItem["obsdate"].InnerXml;
_forecast.day_text = _dayItem.SelectSingleNode("pf:daytime", _mgr)["txtshort"].InnerXml;
_forecast.day_icon =
Convert.ToInt32(_dayItem.SelectSingleNode("pf:daytime", _mgr)["weathericon"].InnerXml);
_forecast.day_htemp =
Convert.ToInt32(_dayItem.SelectSingleNode("pf:daytime", _mgr)["hightemperature"].InnerXml);
_forecast.day_ltemp =
Convert.ToInt32(_dayItem.SelectSingleNode("pf:daytime", _mgr)["lowtemperature"].InnerXml);
_weather.forecast.Add(_forecast);
i++;
// 5 day forecast
if (i == 5) break;
}
Response.Write(Newtonsoft.Json.JsonConvert.SerializeObject(_weather));
}
/// <summary>
/// Returns the content of a given web adress as string.
/// </summary>
/// <param name="Url">URL of the webpage</param>
/// <returns>Website content</returns>
public string DownloadWebPage(string Url)
{
// Open a connection
HttpWebRequest WebRequestObject = (HttpWebRequest)HttpWebRequest.Create(Url);
// You can also specify additional header values like
// the user agent or the referer:
WebRequestObject.UserAgent = ".NET Framework/2.0";
WebRequestObject.Referer = "http://www.example.com/";
// Request response:
WebResponse Response = WebRequestObject.GetResponse();
// Open data stream:
Stream WebStream = Response.GetResponseStream();
// Create reader object:
StreamReader Reader = new StreamReader(WebStream);
// Read the entire stream content:
string PageContent = Reader.ReadToEnd();
// Cleanup
Reader.Close();
WebStream.Close();
Response.Close();
return PageContent;
}
}
public class Weather
{
public string city;
public int curr_temp;
public string curr_text;
public int curr_icon;
public List<Forecast> forecast = new List<Forecast>();
}
public class Forecast
{
public string day_date;
public string day_text;
public int day_icon;
public int day_htemp;
public int day_ltemp;
}
}
//------------------------------------------------------------------------------
// <auto-generated>
// This code was generated by a tool.
// Runtime Version:2.0.50727.4927
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>
//------------------------------------------------------------------------------
namespace MultiConsultCRM.Web.Resources {
public partial class WeatherProxy {
}
}
# Ignore everything in this directory
*
# Except this file !.gitkeep
\ No newline at end of file
<?php
$location = $_GET['location'];
$metric = (int)$_GET['metric'];
$url = 'http://wwwa.accuweather.com/adcbin/forecastfox/weather_data.asp?location=' . $location . '&metric=' . $metric;
//$url = 'http://rainmeter.accu-weather.com/widget/rainmeter/weather-data.asp?location=' . $location . '&metric=' . $metric;
$ch = curl_init();
$timeout = 0;
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
$xml = simplexml_load_string($file_contents);
$weather['city'] = (string)$xml->local->city;
$weather['curr_temp'] = (int)$xml->currentconditions->temperature;
$weather['curr_text'] = (string)$xml->currentconditions->weathertext;
$weather['curr_icon'] = (int)$xml->currentconditions->weathericon;
// forecast
//$day = count($xml->forecast->day);
$day = 5;
for ($i = 0; $i < $day; $i++) {
$weather['forecast'][$i]['day_date'] = (string)$xml->forecast->day[$i]->obsdate;
$weather['forecast'][$i]['day_text'] = (string)$xml->forecast->day[$i]->daytime->txtshort;
$weather['forecast'][$i]['day_icon'] = (int)$xml->forecast->day[$i]->daytime->weathericon;
$weather['forecast'][$i]['day_htemp'] = (int)$xml->forecast->day[$i]->daytime->hightemperature;
$weather['forecast'][$i]['day_ltemp'] = (int)$xml->forecast->day[$i]->daytime->lowtemperature;
}
echo json_encode($weather);
?>
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment