Weather forecast interface in the Web page application of many, generally in the Discuz website open backstage can be added, but how to implement the programming language?
can apply to SMARTWEATHERAPI weather the use of the interface, began to start my real-time weather forecast system development, the main development of the version using a Python script, the results will be in a series of articles to meet with you recently. Here today I and you discuss the SMARTWEATHERAPI key calculation method, and provide C + + program source code for your reference.
SMARTWEATHERAPI ("SWA Interface") is the official carrier of the China Meteorological Bureau for Internet media, mobile phone manufacturers, third-party meteorological service organizations and other users, through the Web way to provide data meteorological services. Is the first in the country for personal sites, development enthusiasts and service providers of meteorological Services API Data Development interface (application address stamp). The application process needs to fill out a form, sent to the official mailbox, the manual review will reply to the mail, with the letter to provide AppID and private_key information.
The contents of the reply message are as follows (AppID and Private_key are private information, so they are replaced with red horizontal lines):
private_key:接口使用说明请参考《SmartWeatherAPI_Lite_WebAPI 版产品使用说明书》,区域列表:请见附件areaid_list.xlsx。该鉴权信息仅限您个人或本公司使用,如有泄露我们将撤销您的使用权限,必要时将追究相关责任。最后,非常感谢您的参与。
Interface Description said in the above mentioned API interface application website can be downloaded to, below on the instructions of the following several points to do:
- Interface description
Request method: HTTP GET
Interface consists of a fixed URL plus 5 different parameters, the full URL needs to be fixed by the client after the use of encryption.
Data return: JSON
Full url:http://webapi.weather.com.cn/data/?= "" &= "" &= "" &= "" &= ". UrlEncode ($key)
Input parameters:
Areaid: The zone ID, which has an attachment in the post-approval message, provides the ID number of all existing zones.
Type: Data type (live: Observe, Index: Index, General forecast: Forecast3d).
Date: The client's current time, in format yyyymmddhhmm.
AppID: Fixed assigned model identification, after the audit passed the message (When passing parameters: intercept the first 6 bits of the AppID; when generating the public key: Take the full AppID).
Key: Token, the public key (Public_key) and the private key (Private_key) are generated by the fixed algorithm encryption.
- Encryption method
Encryption algorithm is the basis of today's blog, we read carefully. The three elements involved in a cryptographic algorithm are:
Private_key: After approval, the message will be provided. Private_key is only responsible for the co-synthesis of key parameters with Public_key, the private key is not visible, the client and the server store one copy;
Public_key: Does not contain key in the other part of the full URL of the class (here AppID is the full appid);
Key algorithm: The manual provides the algorithm code in PHP, as follows
= (Hash_hmac (' SHA1 ',,,));
After the key is encrypted, it is encoded by UrlEncode and then the parameter is passed.
For example, the URL to remove the key section is as follows:
Http://webapi.weather.com.cn/data/?areaid=101010100&type=index&date=201211281030&appid= Cf2d61521456sads
The private key Private_key is assumed to be: Private_key
We use the above algorithm to first calculate the encrypted key, after we get the key using the UrlEncode method to process the key we encrypt, the result is:
A%2fp2qj4r%2fd3ffcr6xwucynp56y0%3d
Then our final input URL is:
http://webapi.weather.com.cn/data/?areaid=101010100&type=index&date=201211281030&appid=cf2d61& Key=a%2fp2qj4r%2fd3ffcr6xwucynp56y0%3d
- Return data
Output instance:
}
which
L1: Indicates the current temperature (Celsius);
L2: Indicates the current humidity (%);
L3: Indicates the current wind (level);
L4: Indicates the current wind direction number (the homepage has literature to find out the specific wind direction);
L7: Indicates the live release time.
The current different type request results are not quite the same, the other 2 types of I do not explain here, need to refer to the official documentation, here by the way a Type=forecast3d return results:
} View Code
Second, the encryption process analysis
Before I implemented the encryption method, I was thinking that first I should know that the correct encryption results are being sprinkled, so that I can see the correct return data before doing this thing is not more meaningful. Look at the official offer is the PHP method, then I will try to use PHP first, but I do not install PHP, always do not because of a small test of the entire PHP environment, trouble! Fortunately there is php online to help me http://writecodeonline.com/php/:
The result in the above example is this:
(Hash_hmac (' SHA1 ', "http://webapi.weather.com.cn/data/?areaid=101010100&type=index&date=201211281030 &appid=cf2d61521456sads "," Private_key ",)));
Of course, the URL in the instance does not return the correct data, because the AppID and private_key that we use are bogus, and I use the information I applied for in the testing process.
The result returned successfully, and we began to analyze how to use C + + to implement this encryption process!
- Dismember the encryption process
One bite can not eat a fat, we step-by-step, from the most inner layer, starting from the Hash_hmac, White is not the hash process, this time I think of OpenSSL, yes, it is him! First we find out the results of our step in the PHP environment:
Hash_hmac (' SHA1 ', "http://webapi.weather.com.cn/data/?areaid=101010100&type=index&date=201211281030 &appid=cf2d61521456sads "," Private_key ",);
The results are as follows:
[email protected]?? =?*?_?????
Before using OpenSSL, let's analyze some of the parameters in the Hash_hmac function in PHP below, the first three estimates at a glance, what does the last one mean? The function prototypes are as follows:
Hash_hmac (,, [, bool =])
The last parameter, Raw_output, is interpreted as: When set to TRUE, outputs raw binary data. FALSE outputs lowercase hexits.
OK, let's start with OpenSSL and try it out in C + + and try it with the command under bash first:
-N | OpenSSL Dgst-sha1-binary-hmac
See here the-binary parameter is not, this is why I was specifically to analyze the reason for Hash_hmac last parameter, because I was here in the absence of-binary parameters, resulting in the result is always not on. Well, the results show that the results are consistent with PHP.
Continue to Dismember, start the second step, Base64_encode function analysis, again, first in PHP:
(Hash_hmac (' SHA1 ', "http://webapi.weather.com.cn/data/?areaid=101010100&type=index&date=201211281030 &appid=cf2d61521456sads "," Private_key ",));
The results are as follows:
a/p2qj4r/d3ffcr6xwucynp56y0=
Let's take a look at the prototype of the Base64_encode function:
( )
The function, known as Base64, is to encode the data. There are ready-made tools under Linux, namely Base64:
-N | OpenSSL Dgst-sha1-binary-hmac | Base64
The test passes, we begin the final step, what the UrlEncode function does, and this question is mentioned in a previous article: Http://www.cnblogs.com/berlin-sun/p/translateonline.html, In fact, the URL is re-encoded, Returns a string in which all non-alphanumeric characters except-_. There are been replaced with a percent (%) sign followed by both hex digits and spaces encoded as plus (+) signs.
- Specific implementation
HMAC process:
Include
unsigned * digest = HMAC (EVP_SHA1 (), Key, strlen (key), (unsigned *) Weather_api, strlen (WEATHER_API), NULL, NULL);
Base64 Process:
IncludeInclude
Base64 (unsigned * input, BIO bmem, * buf_mem *
b64 = bmem = b64 = BIO_get_mem_ptr(b64, & *buff = ( *)malloc(bptr-> memcpy(buff, bptr->data, bptr->length- buff[bptr->length-] =
base64digest = base64 (Digest, strlen (( ) digest));
UrlEncode process:
Inlcude "curl/
Curl * Curl =
*encode_key = Curl_easy_escape (curl, base64digest,);
Currently, the C + + program has completed these and the results have been successfully returned, such as:
Feel the world of programming.
How programming language is placed into the weather forecast interface API