hash_hmac 签名
<?php
/**
* ===========================================================
* Model_Base
* Description
* Model_Base
* @Author wzhu.email@gmail.com
* @Version 1.0
* @Copyright Zhuweiwei
* Copyright © 2008-2012
* China. All Rights Reserved.
* ===========================================================
*/ namespace Action; use ZW\Utility as Utility;
use ZW\Http\Client as HttpClient; class Expo
{
CONST URL_BASE = 'https://api.webexpotec.cn'; CONST URL_EVENT = '/v1/event';
//登录
CONST URL_AUTH_SIGNIN = '/v1/auth/signin';
//获取验证码和验证验证码
CONST URL_AUTH_VCODE = '/v1/auth/vcode';
//下订单
CONST URL_ORDER = '/v1/order';
//生成凭证
CONST URL_ADMISSION = '/v1/admission';
//查询用户是否已经有凭证
CONST URL_ADMISSION_LISTING = '/v1/admission/listing';
//二维码
CONST URL_QR = '/v1/qr';
//问卷调查
CONST URL_QURESTION = '/v1/question/listing';
//提交问题
CONST URL_SURVEY = '/v1/survey/submit';
//lifecbd 接口路径
CONST URL_ARTICLE = '/v1/article'; /* protected $app_key = '1467721313';
protected $app_secret = '21e122dba62dcd9aed1488aced61f2c2';
protected $eventid = '1440860341';//
protected $ticketid = '1463800736';//票务ID*/ protected $app_key = '1467721309';
protected $app_secret = '567aa793d2cfb865eec8623dc7b9de92';
protected $eventid = '1440860340';//活动id
protected $ticketid = '000000';//票务ID--随便写 protected $app_ver = '1.0.0';
protected $app_uuid = 'ABCDEFG';
protected $sig_method = 'HMAC-SHA1';
protected $il8n_lang = 'zh-cn'; public function decodeQrcode($string)
{
$arrParam = array(
'string' => $string,
);
return $this->get(self::URL_QR, $arrParam);
} public function getSignin($arrParam = array())
{
$arrParam = array(
'username' => '13436919765',
'password' => '919765',
'countrycode' => 'CN',
);
return $this->get(self::URL_AUTH_SIGNIN, $arrParam);
} //登录
public function getEventListing(array $arrParam = array(), $start = 0, $num = 100, $scope = 0)
{
$arrParam['start'] = (int)$start;
$arrParam['num'] = (int)$num;
$arrParam['scope'] = $scope;
$url = sprintf('%s/listing', self::URL_EVENT);
return $this->get($url, $arrParam);
} //获取活动内容
public function getEvent($eventId, $scopes = 1)
{
$url = sprintf('%s/%d', self::URL_EVENT, $eventId);
$arrParam = array(
'id' => (int)$eventId,
'scopes' => (int)$scopes,
);
return $this->get($url, $arrParam);
} //获取验证码
public function getAuthVcode($username, $type = 4, $countryCode = 'CN')
{
$arrParam = array(
'username' => $username,
'countrycode' => $countryCode,
'type' => $type,
);
return $this->get(self::URL_AUTH_VCODE, $arrParam);
} /**
*读取列表数据
*/
public function getArticleList($channel, $start = 0, $num = 20, $type = 0){ $arrParam = array(
'channel' => $channel,
'type' => $type,
'start' => $start,
'num' => $num,
);
$url = sprintf('%s/listing', self::URL_ARTICLE);
return $this->get($url, $arrParam);
} /**读取文章数据
* @param $articleId 文章id
*/
public function getArticle($articleId){
$url = sprintf('%s/%d', self::URL_ARTICLE, $articleId);
return $this->get($url, array());
} //校验验证码
public function chkAuthVcode($username, $vcode = '', $type = 0, $countryCode = 'CN')
{
if (empty($vcode)) {
$vcode = "abcdfg";
}
$arrParam = array(
'username' => $username,
'countrycode' => $countryCode,
'type' => $type,
'vcode' => $vcode,
);
return $this->post(self::URL_AUTH_VCODE, $arrParam);
} //提交注册表单
public function orderForm($arr)
{
$goods = [
'id' => $arr['id'],
'num' => $arr['num'],
];
$goods = '[' . json_encode($goods) . ']';
$arrParam = array(
'type' => $arr['type'],
'goods' => $goods,
'realname' => $arr['realname'],
'cellphone' => $arr['cellphone'],
'email' => $arr['email'],
'corp' => $arr['corp'],
'title' => $arr['title'],
'address' => $arr['address'],
'countrycode' => $arr['countrycode'],
'adminmemo' => $arr['adminmemo'],
'promocode' => $arr['promocode'],
);
return $this->post(self::URL_ORDER, $arrParam);
} //检查订单状态
public function chkOrder($orderid)
{
$arrParam = array(
'orderid' => $orderid,
);
return $this->get(self::URL_ORDER . "/" . $orderid, $arrParam);
} //注册凭证
public function registerAdmission($ticketnoid, $arr)
{ $arrParam = array(
'ticketno' => $ticketnoid,
'realname' => $arr['realname'],
'cellphone' => $arr['cellphone'],
'email' => $arr['email'],
'corp' => $arr['corp'],
'title' => $arr['title'],
'countrycode' => 'CN',
'extdata' => $arr['adminmemo'],
);
return $this->post(self::URL_ADMISSION, $arrParam);
} public function getUserAdmissionList($arrParam)
{ $arrParam['eventid'] = $this->eventid;
$arrParam['ticketid'] = $this->ticketid; return $this->get(self::URL_ADMISSION_LISTING, $arrParam);
} //获取问题列表
public function getQuestionList($token, $suveryid)
{
$arrParam = array('surveyid' => (int)$suveryid,
'token' => $token,);
return $this->get(self::URL_QURESTION, $arrParam);
} //提交问题列表
public function submitSuvery($id, $answers)
{
return $this->post(self::URL_SURVEY . '/' . $id, $answers);
} ////////////////////////////////////////////////////////////////////////// //stdClass Object转array
public function objectArray($array)
{
if (is_object($array)) {
$array = (array)$array;
} else if (is_array($array)) {
foreach ($array as $key => $value) {
$array[$key] = $this->objectArray($value);
}
}
return $array;
} //get组装curl字符串
protected function urlGetstr($url, $arrParam)
{
$url = $url . "?" . $this->arrayTostr($arrParam);
return $url;
} //curl调用接口
protected function httpCurlExce($url, $arrParam = array(), $method)
{
//如果是get提交需要组装url,post的则不需要
if ($method == 'get') {
$url = $this->urlGetstr($url, $arrParam);
}
//echo $url;
//curl发送
$curl = curl_init();
//设置抓取的url
curl_setopt($curl, CURLOPT_URL, $url);
//设置头文件的信息作为数据流输出
curl_setopt($curl, CURLOPT_HEADER, 1);
//设置获取的信息以文件流的形式返回,而不是直接输出。
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
//绕过ssl验证
//curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//在尝试连接时等待的秒数
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 90);
//最大执行时间
curl_setopt($curl, CURLOPT_TIMEOUT, 90);
if ($method == 'post') {
//设置post方式提交
curl_setopt($curl, CURLOPT_POST, 1);
//设置post数据
curl_setopt($curl, CURLOPT_POSTFIELDS, $arrParam);
}
//执行命令
$data = curl_exec($curl);
//处理头信息
// 获得响应结果里的:头大小
$headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
// 根据头大小去获取头信息内容
$ret = substr($data, $headerSize);
//关闭URL请求
curl_close($curl);
//stdClass Object转array
$ret = json_decode($ret);
$ret = $this->objectArray($ret);
//echo "<pre>";
//print_r($data);
return $ret;
} //签名算法
protected function getSignature($str, $key)
{
$signature = "";
if (function_exists('hash_hmac')) {
//$signature = bin2hex(hash_hmac("sha1", $str, $key, true));
$signature = hash_hmac("sha1", $str, $key, true);
} else {
$blocksize = 64;
$hashfunc = 'sha1';
if (strlen($key) > $blocksize) {
$key = pack('H*', $hashfunc($key));
}
$key = str_pad($key, $blocksize, chr(0x00));
$ipad = str_repeat(chr(0x36), $blocksize);
$opad = str_repeat(chr(0x5c), $blocksize);
$hmac = pack(
'H*', $hashfunc(
($key ^ $opad) . pack(
'H*', $hashfunc(
($key ^ $ipad) . $str
)
)
)
);
//$signature = bin2hex($hmac);
$signature = $hmac;
}
return $signature;
} //base64UrlEncode
protected function base64UrlEncode($str)
{
$search = array('+', '/', '=');
$replace = array('*', '-', '');
$basestr = base64_encode($str);
return str_replace($search, $replace, $basestr);
} /**
* 将数组中的值,按照键值排序,之后将每个元素拼接成url样式
* @param $arrParam:数组
* @return $str : 拼接的url地址
*/
protected function arrayTostr($arrParam)
{
$i = $arr_num = 0;
$str = "";
ksort($arrParam, SORT_NATURAL);
$arr_num = count($arrParam);
foreach ($arrParam as $key => $val) {
$i++;
if ($i < $arr_num) {
$str .= $key . '=' . urlencode($val) . '&';
} else {
$str .= $key . '=' . urlencode($val);
}
}
return $str;
} protected function buildSignature($arrParam)
{
$str = $this->arrayTostr($arrParam);//url地址拼接
$signature = $this->getSignature($str, $this->app_secret);//hash_hmac 签名
$signature = $this->base64UrlEncode($signature);//base64加密
return $signature;
} protected function getRandStr()
{
$str = '';
for ($i = 1; $i <= 10; $i++) {
$str .= chr(rand(65, 90));
}
return $str;
} protected function adaptSysParam(array $arrParam)
{
$arrParam['app_key'] = $this->app_key;
$arrParam['timestamp'] = NOW_TIME;
$arrParam['sig_method'] = $this->sig_method;
$arrParam['app_uuid'] = $this->app_uuid;
$arrParam['app_ver'] = '1.0.0';
$arrParam['il8n_lang'] = $this->il8n_lang;
$arrParam['req_uuid'] = $this->getRandStr();
$arrParam['signature'] = $this->buildSignature($arrParam);
return $arrParam;
} /**
* @param :$url url地址
* @param :adaptSysParam 组合需要的传参,最后生成签名:签名方法:将数组组成url参数,生成hmac签名,base64加密
* @param :httpCurlExce curl获取接口内容
*/
protected function get($url, array $arrParam)
{
$arrParam = $this->adaptSysParam($arrParam);
$arr = $this->httpCurlExce(self::URL_BASE . $url, $arrParam, 'get');
return $arr;
} protected function post($url, array $arrParam)
{
$arrParam = $this->adaptSysParam($arrParam);
$arr = $this->httpCurlExce(self::URL_BASE . $url, $arrParam, 'post');
return $arr;
} }
hash_hmac 签名的更多相关文章
- HMAC-SHA1算法签名及Authorization头认证
使用PHP进行HMAC-SHA1签名,并通过Authorization头认证Deom $app_id = 'id'; $host = "test.abc.com"; $port = ...
- 关于下载SAE日志签名认证的方法——PHP版
之前需要下载SAE上的日志存入数据库,因此研究了下SAE的签名认证和日志下载.这个链接是SAE官方给出的API文档.https://www.sinacloud.com/doc/api.html#qia ...
- PHP生成腾讯云COS请求签名
目标 使用 PHP 创建 COS 接口所需要的请求签名 步骤 按照官方示例(也许是我笨,我怎么读都觉得官方文档结构费劲,示例细节互相不挨着,容易引起歧义),请求签名应用在需要身份校验的场景,即非公有读 ...
- php签名认证
一.概述 开年第一篇,该篇主要讲述了接口开发中,如何安全认证.如何用php签名认证. 二.说说历史 签名认证是什么?为什么要做签名认证?签名认证哪里会用到?no.no.no.....是不是,是不是,一 ...
- PHP HMAC_SHA1 算法 生成算法签名
HMAC_SHA1(Hashed Message Authentication Code, Secure Hash Algorithm)是一种安全的基于加密hash函数和共享密钥的消息认证协议. 它可 ...
- 认证加密算法php hash_hmac和java hmacSha1的问题
public class Test{ public static void main(String[] args) throws Exception { String postString = &qu ...
- 阿里云OSS Web端直传 服务器签名C#版
最近用到队里OSS的文件上传,然后阿里官方给的四个服务器签名有Java PHP Python Go四个版本,就是没C#(话说写个C#有多难?) 百度了一下好像也没有,既然这样只能自己动手照着Java版 ...
- python通过sha1和base64生成签名
一.python生成时间戳 # 案例 生成时间戳 import time t = time.time() print(t) # 原始时间数据 print(int(t)) # 秒级时间戳 print(i ...
- 微信退款通知,退款回调数据解密.SHA256签名AEAD_AES_256_GCM解密
$xmlResult = file_get_contents("php://input");//获取微信的数据$result = $this->xmlToArray($xml ...
随机推荐
- 前端数据可视化echarts.js
一.echarts.js的优势与总体情况 echarts.js作为国内的IT三巨头之一的百度的推出一款相对较为成功的开源项目,总体上来说有这样的一些优点 1.容易使用 echarts.js的官方文档比 ...
- 解密JavaScript闭包
译者按: 从最简单的计数器开始,按照需求对代码一步步优化,我们可以领会闭包的神奇之处. 原文: Closures are not magic 译者: Fundebug 为了保证可读性,本文采用意译而非 ...
- chorme type=file 延迟5-8秒
原文地址:http://www.piaoyi.org/computer/Google-Chrome-input-file-delay-3-5.html 原先写法: <button type=&q ...
- K8S RBAC
API Server 内部通过用户认证后,然后进入授权流程.对合法用户进行授权并且随后在用户访问时进行鉴权,是权限管理的重要环节. 在 kubernetes 集群中,各种操作权限是赋予角色(Role ...
- vue+vuecli+webapck2项目配置文件详解
1.文件结构 ├─build │ ├─build.js │ ├─check-versions.js │ ├─dev-client.js │ ├─dev-server.js │ ├─utils.js │ ...
- Openlayer3中应用的技术
ol3-ext有很多很丰富的效果,可以不用重复造轮子,ol3-ext示例大全:http://viglino.github.io/ol3-ext/ 在本次项目中使用到了ol3-ext的两个功能:图层管理 ...
- 13.Odoo产品分析 (二) – 商业板块(6) –采购(3)
接上一篇 查看Odoo产品分析系列--目录 接上一篇Odoo产品分析 (二) – 商业板块(6) –采购(2) 7. 仓库 仓库是在安装采购管理模块时出现的菜单.用于管理工厂库存,包括已经在手的货物 ...
- 将html前端代码提取公因数(5)
将html前端代码提取公因数(5) 注意:这是优化html代码,对于多个html代码相同的部分提取到一个模板中,只需要编写变化的html 1,利用Django提供的render方法的第三个参数的属性 ...
- Python 反射机制之hasattr()、getattr()、setattr() 、delattr()函数
反射机制 先看看我对Java中反射机制的通俗理解:反射之中包含了一个“反”的概念,所以要想解释反射就必须先从“正”开始解释,一般而言,当用户使用一个类的时候,应该先知道这个类,而后通过这个类产生实例化 ...
- spark RDD,reduceByKey vs groupByKey
Spark中有两个类似的api,分别是reduceByKey和groupByKey.这两个的功能类似,但底层实现却有些不同,那么为什么要这样设计呢?我们来从源码的角度分析一下. 先看两者的调用顺序(都 ...