请选择开发语言:
PHP
Python
$api_url = 'https://登录后显示/api/68/177';
$appid = '应用id';// 在后台我的应用查看;
$secret = '应用密钥';// 在后台我的应用查看;
$data = array(
'appid'=> '1',
'end_time'=> '2019-05-28 17:21',
'format'=> 'json',
'start_time'=> '2019-05-28 10:20',
'time'=> '1545829466',
'weibo_keyword'=> '结婚不举行婚礼',
'weibo_speed'=> '1',
'weibo_top'=> '10',
);
$data['appid'] = $appid;
$data['time'] = time();//当前服务器时间
ksort($data); //按照键名对数组排序,为数组值保留原来的键。
$md5String = '';
foreach($data as $key=>$val){
if(strlen($val)>0){ //过滤空值
$md5String.=$key.$val;
}
}
$sign = md5($md5String.$secret);
$data['sign'] = $sign;
$sendUrl = $api_url.'?'.http_build_query($data); //把数据转换成url参数形式,a=b&c=d&e=f
$result = file_get_contents($sendUrl);
print_r($result);
#!/usr/bin/python3
# -*- coding: UTF-8 -*-
#该代码为python3.7示例代码
import ssl,time,hashlib,string
from urllib import request,parse,error;
from urllib.parse import quote
api_url = 'https://登录后显示/api/68/177';
appid = '应用id';#在后台我的应用查看;
secret = '应用密钥';#在后台我的应用查看;
data = {
'appid':'1',
'end_time':'2019-05-28 17:21',
'format':'json',
'start_time':'2019-05-28 10:20',
'time':'1545829466',
'weibo_keyword':'结婚不举行婚礼',
'weibo_speed':'1',
'weibo_top':'10',
};
data['appid'] = appid;
data['time'] = round(time.time());#当前服务器时间
keysArr = list(data.keys()) #取出字典key
keysArr.sort()#对字典key进行排序
md5String = '';
params = []
for key in keysArr:
if data[key]:
val = str(data[key])
md5String+=key+val
params.append(key + "=" + val )
md5String += secret;
m = hashlib.md5()
b = md5String.encode(encoding='utf-8')
m.update(b)
sign = m.hexdigest()
params.append('sign='+sign)#加入计算后的sign值去请求
params = '&'.join(tuple(params)); #把列表转成元组后用&分隔,最终转换成字符串 a=b&c=d&e=f
ssl._create_default_https_context = ssl._create_unverified_context
url = api_url+'?'+params;
url = quote(url, safe=string.printable)
req = request.Request(url)
opener = request.build_opener()
r = opener.open(fullurl=req)
doc = r.read();