Beispiel Abfrage Yahoo Weather fuer Zuerich

Dependencies:   EthernetInterface mbed-rtos mbed spxml

Fork of HTTP_GET by smd.iotkit2.ch

Mittels Yahoo Weather kann das aktuelle Wetter und eine Vorhersage für einen Bestimmten Ort abgefragt werden.

Links

Beispiel Abfrage Wetter in Zürich

Aufruf mittels curl:

    curl http://query.yahooapis.com/v1/public/yql  \
           -d q="select * from weather.forecast where woeid=784794"    -d format=xml

Antwort als XML von Yahoo:

<?xml version="1.0" encoding="UTF-8"?>
<query xmlns:yahoo="http://www.yahooapis.com/v1/base.rng" yahoo:count="1" yahoo:created="2016-07-22T13:17:05Z"
	yahoo:lang="en-US">
	<results>
		<channel>
			<yweather:units xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" distance="mi" pressure="in"
				speed="mph" temperature="F" />
			<title>Yahoo! Weather - Zurich, ZH, CH</title>
			<link>http://us.rd.yahoo.com/dailynews/rss/weather/Country__Country/*https://weather.yahoo.com/country/state/city-784794/</link>
			<description>Yahoo! Weather for Zurich, ZH, CH</description>
			<language>en-us</language>
			<lastBuildDate>Fri, 22 Jul 2016 03:17 PM CEST</lastBuildDate>
			<ttl>60</ttl>
			<yweather:location xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" city="Zurich"
				country="Switzerland" region=" ZH" />
			<yweather:wind xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" chill="72" direction="150"
				speed="11" />
			<yweather:atmosphere xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" humidity="72"
				pressure="957.0" rising="0" visibility="16.1" />
			<yweather:astronomy xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" sunrise="5:54 am"
				sunset="9:10 pm" />
			<image>
				<title>Yahoo! Weather</title>
				<width>142</width>
				<height>18</height>
				<link>http://weather.yahoo.com</link>
				<url>http://l.yimg.com/a/i/brand/purplelogo//uh/us/news-wea.gif</url>
			</image>
			<item>
				<title>Conditions for Zurich, ZH, CH at 02:00 PM CEST</title>
				<geo:lat xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#">47.38427</geo:lat>
				<geo:long xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#">8.52959</geo:long>
				<link>http://us.rd.yahoo.com/dailynews/rss/weather/Country__Country/*https://weather.yahoo.com/country/state/city-784794/</link>
				<pubDate>Fri, 22 Jul 2016 02:00 PM CEST</pubDate>
				<yweather:condition xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" code="39"
					date="Fri, 22 Jul 2016 02:00 PM CEST" temp="72" text="Scattered Showers" />
				<yweather:forecast xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" code="4" date="22 Jul 2016"
					day="Fri" high="77" low="66" text="Thunderstorms" />
				<yweather:forecast xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" code="47" date="23 Jul 2016"
					day="Sat" high="76" low="64" text="Scattered Thunderstorms" />
				<yweather:forecast xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" code="39" date="24 Jul 2016"
					day="Sun" high="78" low="63" text="Scattered Showers" />
				<yweather:forecast xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" code="47" date="25 Jul 2016"
					day="Mon" high="77" low="64" text="Scattered Thunderstorms" />
				<yweather:forecast xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" code="47" date="26 Jul 2016"
					day="Tue" high="75" low="64" text="Scattered Thunderstorms" />
				<yweather:forecast xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" code="4" date="27 Jul 2016"
					day="Wed" high="75" low="61" text="Thunderstorms" />
				<yweather:forecast xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" code="30" date="28 Jul 2016"
					day="Thu" high="76" low="60" text="Partly Cloudy" />
				<yweather:forecast xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" code="30" date="29 Jul 2016"
					day="Fri" high="79" low="59" text="Partly Cloudy" />
				<yweather:forecast xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" code="39" date="30 Jul 2016"
					day="Sat" high="80" low="58" text="Scattered Showers" />
				<yweather:forecast xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0" code="30" date="31 Jul 2016"
					day="Sun" high="78" low="59" text="Partly Cloudy" />
				<description></description>
				<guid isPermaLink="false" />
			</item>
		</channel>
	</results>
</query>
Committer:
marcel1691
Date:
Sat Jan 17 14:11:01 2015 +0000
Revision:
1:2e29a33cd918
Beispiel fuer Verwendung der HTTP Methoden (GET, POST, PUT, DELETE)

Who changed what in which revision?

UserRevisionLine numberNew contents of line
marcel1691 1:2e29a33cd918 1 /* HTTPClient.h */
marcel1691 1:2e29a33cd918 2 /* Copyright (C) 2012 mbed.org, MIT License
marcel1691 1:2e29a33cd918 3 *
marcel1691 1:2e29a33cd918 4 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
marcel1691 1:2e29a33cd918 5 * and associated documentation files (the "Software"), to deal in the Software without restriction,
marcel1691 1:2e29a33cd918 6 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
marcel1691 1:2e29a33cd918 7 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
marcel1691 1:2e29a33cd918 8 * furnished to do so, subject to the following conditions:
marcel1691 1:2e29a33cd918 9 *
marcel1691 1:2e29a33cd918 10 * The above copyright notice and this permission notice shall be included in all copies or
marcel1691 1:2e29a33cd918 11 * substantial portions of the Software.
marcel1691 1:2e29a33cd918 12 *
marcel1691 1:2e29a33cd918 13 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
marcel1691 1:2e29a33cd918 14 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
marcel1691 1:2e29a33cd918 15 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
marcel1691 1:2e29a33cd918 16 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
marcel1691 1:2e29a33cd918 17 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
marcel1691 1:2e29a33cd918 18 */
marcel1691 1:2e29a33cd918 19
marcel1691 1:2e29a33cd918 20 /** \file
marcel1691 1:2e29a33cd918 21 HTTP Client header file
marcel1691 1:2e29a33cd918 22 */
marcel1691 1:2e29a33cd918 23
marcel1691 1:2e29a33cd918 24 #ifndef HTTP_CLIENT_H
marcel1691 1:2e29a33cd918 25 #define HTTP_CLIENT_H
marcel1691 1:2e29a33cd918 26
marcel1691 1:2e29a33cd918 27 #include "TCPSocketConnection.h"
marcel1691 1:2e29a33cd918 28
marcel1691 1:2e29a33cd918 29 #define HTTP_CLIENT_DEFAULT_TIMEOUT 15000
marcel1691 1:2e29a33cd918 30
marcel1691 1:2e29a33cd918 31 class HTTPData;
marcel1691 1:2e29a33cd918 32
marcel1691 1:2e29a33cd918 33 #include "IHTTPData.h"
marcel1691 1:2e29a33cd918 34 #include "mbed.h"
marcel1691 1:2e29a33cd918 35
marcel1691 1:2e29a33cd918 36 ///HTTP client results
marcel1691 1:2e29a33cd918 37 enum HTTPResult {
marcel1691 1:2e29a33cd918 38 HTTP_OK = 0, ///<Success
marcel1691 1:2e29a33cd918 39 HTTP_PROCESSING, ///<Processing
marcel1691 1:2e29a33cd918 40 HTTP_PARSE, ///<url Parse error
marcel1691 1:2e29a33cd918 41 HTTP_DNS, ///<Could not resolve name
marcel1691 1:2e29a33cd918 42 HTTP_PRTCL, ///<Protocol error
marcel1691 1:2e29a33cd918 43 HTTP_NOTFOUND, ///<HTTP 404 Error
marcel1691 1:2e29a33cd918 44 HTTP_REFUSED, ///<HTTP 403 Error
marcel1691 1:2e29a33cd918 45 HTTP_ERROR, ///<HTTP xxx error
marcel1691 1:2e29a33cd918 46 HTTP_TIMEOUT, ///<Connection timeout
marcel1691 1:2e29a33cd918 47 HTTP_CONN, ///<Connection error
marcel1691 1:2e29a33cd918 48 HTTP_CLOSED, ///<Connection was closed by remote host
marcel1691 1:2e29a33cd918 49 };
marcel1691 1:2e29a33cd918 50
marcel1691 1:2e29a33cd918 51 /**A simple HTTP Client
marcel1691 1:2e29a33cd918 52 The HTTPClient is composed of:
marcel1691 1:2e29a33cd918 53 - The actual client (HTTPClient)
marcel1691 1:2e29a33cd918 54 - Classes that act as a data repository, each of which deriving from the HTTPData class (HTTPText for short text content, HTTPFile for file I/O, HTTPMap for key/value pairs, and HTTPStream for streaming purposes)
marcel1691 1:2e29a33cd918 55 */
marcel1691 1:2e29a33cd918 56 class HTTPClient
marcel1691 1:2e29a33cd918 57 {
marcel1691 1:2e29a33cd918 58 public:
marcel1691 1:2e29a33cd918 59 ///Instantiate the HTTP client
marcel1691 1:2e29a33cd918 60 HTTPClient();
marcel1691 1:2e29a33cd918 61 ~HTTPClient();
marcel1691 1:2e29a33cd918 62
marcel1691 1:2e29a33cd918 63 /**
marcel1691 1:2e29a33cd918 64 Provides a basic authentification feature (Base64 encoded username and password)
marcel1691 1:2e29a33cd918 65 Pass two NULL pointers to switch back to no authentication
marcel1691 1:2e29a33cd918 66 @param[in] user username to use for authentication, must remain valid durlng the whole HTTP session
marcel1691 1:2e29a33cd918 67 @param[in] user password to use for authentication, must remain valid durlng the whole HTTP session
marcel1691 1:2e29a33cd918 68 */
marcel1691 1:2e29a33cd918 69 void basicAuth(const char* user, const char* password); //Basic Authentification
marcel1691 1:2e29a33cd918 70
marcel1691 1:2e29a33cd918 71 /**
marcel1691 1:2e29a33cd918 72 Set custom headers for request.
marcel1691 1:2e29a33cd918 73
marcel1691 1:2e29a33cd918 74 Pass NULL, 0 to turn off custom headers.
marcel1691 1:2e29a33cd918 75
marcel1691 1:2e29a33cd918 76 @code
marcel1691 1:2e29a33cd918 77 const char * hdrs[] =
marcel1691 1:2e29a33cd918 78 {
marcel1691 1:2e29a33cd918 79 "Connection", "keep-alive",
marcel1691 1:2e29a33cd918 80 "Accept", "text/html",
marcel1691 1:2e29a33cd918 81 "User-Agent", "Mozilla/5.0 (Windows NT 6.1; WOW64)",
marcel1691 1:2e29a33cd918 82 "Accept-Encoding", "gzip,deflate,sdch",
marcel1691 1:2e29a33cd918 83 "Accept-Language", "en-US,en;q=0.8",
marcel1691 1:2e29a33cd918 84 };
marcel1691 1:2e29a33cd918 85
marcel1691 1:2e29a33cd918 86 http.basicAuth("username", "password");
marcel1691 1:2e29a33cd918 87 http.customHeaders(hdrs, 5);
marcel1691 1:2e29a33cd918 88 @endcode
marcel1691 1:2e29a33cd918 89
marcel1691 1:2e29a33cd918 90 @param[in] headers an array (size multiple of two) key-value pairs, must remain valid during the whole HTTP session
marcel1691 1:2e29a33cd918 91 @param[in] pairs number of key-value pairs
marcel1691 1:2e29a33cd918 92 */
marcel1691 1:2e29a33cd918 93 void customHeaders(const char** headers, size_t pairs);
marcel1691 1:2e29a33cd918 94
marcel1691 1:2e29a33cd918 95 //High Level setup functions
marcel1691 1:2e29a33cd918 96 /** Execute a GET request on the URL
marcel1691 1:2e29a33cd918 97 Blocks until completion
marcel1691 1:2e29a33cd918 98 @param[in] url : url on which to execute the request
marcel1691 1:2e29a33cd918 99 @param[in,out] pDataIn : pointer to an IHTTPDataIn instance that will collect the data returned by the request, can be NULL
marcel1691 1:2e29a33cd918 100 @param[in] timeout waiting timeout in ms (osWaitForever for blocking function, not recommended)
marcel1691 1:2e29a33cd918 101 @return 0 on success, HTTP error (<0) on failure
marcel1691 1:2e29a33cd918 102 */
marcel1691 1:2e29a33cd918 103 HTTPResult get(const char* url, IHTTPDataIn* pDataIn, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking
marcel1691 1:2e29a33cd918 104
marcel1691 1:2e29a33cd918 105 /** Execute a GET request on the URL
marcel1691 1:2e29a33cd918 106 Blocks until completion
marcel1691 1:2e29a33cd918 107 This is a helper to directly get a piece of text from a HTTP result
marcel1691 1:2e29a33cd918 108 @param[in] url : url on which to execute the request
marcel1691 1:2e29a33cd918 109 @param[out] result : pointer to a char array in which the result will be stored
marcel1691 1:2e29a33cd918 110 @param[in] maxResultLen : length of the char array (including space for the NULL-terminating char)
marcel1691 1:2e29a33cd918 111 @param[in] timeout waiting timeout in ms (osWaitForever for blocking function, not recommended)
marcel1691 1:2e29a33cd918 112 @return 0 on success, HTTP error (<0) on failure
marcel1691 1:2e29a33cd918 113 */
marcel1691 1:2e29a33cd918 114 HTTPResult get(const char* url, char* result, size_t maxResultLen, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking
marcel1691 1:2e29a33cd918 115
marcel1691 1:2e29a33cd918 116 /** Execute a POST request on the URL
marcel1691 1:2e29a33cd918 117 Blocks until completion
marcel1691 1:2e29a33cd918 118 @param[in] url : url on which to execute the request
marcel1691 1:2e29a33cd918 119 @param[out] dataOut : a IHTTPDataOut instance that contains the data that will be posted
marcel1691 1:2e29a33cd918 120 @param[in,out] pDataIn : pointer to an IHTTPDataIn instance that will collect the data returned by the request, can be NULL
marcel1691 1:2e29a33cd918 121 @param[in] timeout waiting timeout in ms (osWaitForever for blocking function, not recommended)
marcel1691 1:2e29a33cd918 122 @return 0 on success, HTTP error (<0) on failure
marcel1691 1:2e29a33cd918 123 */
marcel1691 1:2e29a33cd918 124 HTTPResult post(const char* url, const IHTTPDataOut& dataOut, IHTTPDataIn* pDataIn, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking
marcel1691 1:2e29a33cd918 125
marcel1691 1:2e29a33cd918 126 /** Execute a PUT request on the URL
marcel1691 1:2e29a33cd918 127 Blocks until completion
marcel1691 1:2e29a33cd918 128 @param[in] url : url on which to execute the request
marcel1691 1:2e29a33cd918 129 @param[in] dataOut : a IHTTPDataOut instance that contains the data that will be put
marcel1691 1:2e29a33cd918 130 @param[in,out] pDataIn : pointer to an IHTTPDataIn instance that will collect the data returned by the request, can be NULL
marcel1691 1:2e29a33cd918 131 @param[in] timeout waiting timeout in ms (osWaitForever for blocking function, not recommended)
marcel1691 1:2e29a33cd918 132 @return 0 on success, HTTP error (<0) on failure
marcel1691 1:2e29a33cd918 133 */
marcel1691 1:2e29a33cd918 134 HTTPResult put(const char* url, const IHTTPDataOut& dataOut, IHTTPDataIn* pDataIn, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking
marcel1691 1:2e29a33cd918 135
marcel1691 1:2e29a33cd918 136 /** Execute a DELETE request on the URL
marcel1691 1:2e29a33cd918 137 Blocks until completion
marcel1691 1:2e29a33cd918 138 @param[in] url : url on which to execute the request
marcel1691 1:2e29a33cd918 139 @param[in,out] pDataIn : pointer to an IHTTPDataIn instance that will collect the data returned by the request, can be NULL
marcel1691 1:2e29a33cd918 140 @param[in] timeout waiting timeout in ms (osWaitForever for blocking function, not recommended)
marcel1691 1:2e29a33cd918 141 @return 0 on success, HTTP error (<0) on failure
marcel1691 1:2e29a33cd918 142 */
marcel1691 1:2e29a33cd918 143 HTTPResult del(const char* url, IHTTPDataIn* pDataIn, int timeout = HTTP_CLIENT_DEFAULT_TIMEOUT); //Blocking
marcel1691 1:2e29a33cd918 144
marcel1691 1:2e29a33cd918 145 /** Get last request's HTTP response code
marcel1691 1:2e29a33cd918 146 @return The HTTP response code of the last request
marcel1691 1:2e29a33cd918 147 */
marcel1691 1:2e29a33cd918 148 int getHTTPResponseCode();
marcel1691 1:2e29a33cd918 149
marcel1691 1:2e29a33cd918 150 /** Set the maximum number of automated redirections
marcel1691 1:2e29a33cd918 151 @param[in] i is the number of redirections. Values < 1 are
marcel1691 1:2e29a33cd918 152 set to 1.
marcel1691 1:2e29a33cd918 153 */
marcel1691 1:2e29a33cd918 154 void setMaxRedirections(int i = 1);
marcel1691 1:2e29a33cd918 155
marcel1691 1:2e29a33cd918 156 /** get the redirect location url
marcel1691 1:2e29a33cd918 157 @returns const char pointer to the url.
marcel1691 1:2e29a33cd918 158 */
marcel1691 1:2e29a33cd918 159 const char * getLocation() {
marcel1691 1:2e29a33cd918 160 return m_location;
marcel1691 1:2e29a33cd918 161 }
marcel1691 1:2e29a33cd918 162
marcel1691 1:2e29a33cd918 163 private:
marcel1691 1:2e29a33cd918 164 enum HTTP_METH {
marcel1691 1:2e29a33cd918 165 HTTP_GET,
marcel1691 1:2e29a33cd918 166 HTTP_POST,
marcel1691 1:2e29a33cd918 167 HTTP_PUT,
marcel1691 1:2e29a33cd918 168 HTTP_DELETE,
marcel1691 1:2e29a33cd918 169 HTTP_HEAD
marcel1691 1:2e29a33cd918 170 };
marcel1691 1:2e29a33cd918 171
marcel1691 1:2e29a33cd918 172 HTTPResult connect(const char* url, HTTP_METH method, IHTTPDataOut* pDataOut, IHTTPDataIn* pDataIn, int timeout); //Execute request
marcel1691 1:2e29a33cd918 173 HTTPResult recv(char* buf, size_t minLen, size_t maxLen, size_t* pReadLen); //0 on success, err code on failure
marcel1691 1:2e29a33cd918 174 HTTPResult send(char* buf, size_t len = 0); //0 on success, err code on failure
marcel1691 1:2e29a33cd918 175 HTTPResult parseURL(const char* url, char* scheme, size_t maxSchemeLen, char* host, size_t maxHostLen, uint16_t* port, char* path, size_t maxPathLen); //Parse URL
marcel1691 1:2e29a33cd918 176 void createauth (const char *user, const char *pwd, char *buf, int len);
marcel1691 1:2e29a33cd918 177 int base64enc(const char *input, unsigned int length, char *output, int len);
marcel1691 1:2e29a33cd918 178
marcel1691 1:2e29a33cd918 179 //Parameters
marcel1691 1:2e29a33cd918 180 TCPSocketConnection m_sock;
marcel1691 1:2e29a33cd918 181
marcel1691 1:2e29a33cd918 182 int m_timeout;
marcel1691 1:2e29a33cd918 183
marcel1691 1:2e29a33cd918 184 char* m_basicAuthUser;
marcel1691 1:2e29a33cd918 185 char* m_basicAuthPassword;
marcel1691 1:2e29a33cd918 186 const char** m_customHeaders;
marcel1691 1:2e29a33cd918 187 size_t m_nCustomHeaders;
marcel1691 1:2e29a33cd918 188 int m_httpResponseCode;
marcel1691 1:2e29a33cd918 189 int m_maxredirections;
marcel1691 1:2e29a33cd918 190 char * m_location;
marcel1691 1:2e29a33cd918 191
marcel1691 1:2e29a33cd918 192 };
marcel1691 1:2e29a33cd918 193
marcel1691 1:2e29a33cd918 194 //Including data containers here for more convenience
marcel1691 1:2e29a33cd918 195 #include "data/HTTPText.h"
marcel1691 1:2e29a33cd918 196 #include "data/HTTPMap.h"
marcel1691 1:2e29a33cd918 197 #include "data/HTTPFile.h"
marcel1691 1:2e29a33cd918 198
marcel1691 1:2e29a33cd918 199 #endif