SMATERWEATHERAPI---Signature encryption and data access--a simple and brutal step

Source: Internet
Author: User
Tags urlencode

Use China Weather Network interface Friends, everyone (and i) hard

If you are interested, you can get to know http://smart.weather.com.cn/wzfw/smart/weatherapi.shtml.

At the beginning of the year stopped using the previous interface, instead of applying for use, so sent a request for mail, the middle of various twists ... 10,000 words:

Finally the application came down, but the interface document AH, see I feel tired not love, the document only PHP version of the Kay generation method, I use on the Androi, those encryption methods do not know how to implement Java

Rage, just write a PHP version of it, originally was just want to write a generator key interface, and later thought to want to write a few lines directly to a step, eliminating the client on the multi-step network operation and string splicing


For half a day, just look at the effect.


Example:




Full code:

<?php/*----------This file need to be uploaded to their own external network space, no words can be the entire free cloud space I use the magic of the--------------*//*----------role: Access to this file only need to pass type and Areaid These two parameters, can be directly to the weather data/not to go back and forth the signature splicing and so on a lot of trouble things--------------*///China Weather Network SMARTWEATHERAPI Signature interface//signature generated direct access data// Parameter type: request data type; Areaid: City id//appid$app_id= ' your own AppID ';//privatekey$privatekey= ' your own Privatekey '; if (isset request[' type ']) &&isset ($_request[' Areaid ')) {//Receive parameter $type=$_request[' type ']; $areaid =$_request[' Areaid ' ];//formatting the current time yyyymmddhhmm$date=date (' Ymdhi ', Times ())//api request a fixed section $api_head= ' http://open.weather.com.cn/data/? Areaid= '. $areaid. ' &type= '. $type. ' &date= '. $date;//Stitching publickey$publickey= $api _head. ' &appid= '. $app _id;//Generate Key$sign_key=base64_encode (Hash_hmac (' SHA1 ', $publickey, $privatekey, True));// Intercept AppID Top 6 $api_url_appid=substr ($app _id,0,6);//splicing and UrlEncode processing final url$api_url= $api _head. ' &appid= '. $api _url_appid. ' &key= '. UrlEncode ($sign _key);//Save to the end, directly execute the generated URL Access data echo file_get_contents ($api _url);} Else{echo ' error ';}? >


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.