代码:
//签名加密
public function calSignatue($str,$key)
{
$sign = "";
if(function_exists("hash_hmac"))
{
$sign = base64_encode(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
)
)
);
$sign = base64_encode($hmac);
}
return $sign;
}
//消息发布
public function producerProcess($bodyString,$topic,$producerId,$url,$ak,$sk)
{
//请求内容
$request_body = utf8_encode($bodyString);
//$request_body = $bodyString;
//计算时间戳毫秒
$millisecond = time()*1000;
//请求url
$request_url = $url."/message/?topic=".$topic."&time=".$millisecond."&tag=http&key=http";
//签名字符串
$signString = $topic."\n".$producerId."\n".md5($request_body)."\n".$millisecond;
$sign = $this->calSignatue($signString,$sk);
//请求头部
$request_header = [
'Signature:'.$sign,
'AccessKey:'.$ak,
'ProducerId:'.$producerId,
'Content-Type:text/html;charset=UTF-8',
];
//干活
$ch = curl_init();
curl_setopt($ch,CURLOPT_RETURNTRANSFER, 0);
curl_setopt($ch,CURLOPT_HEADER, 1);
curl_setopt($ch,CURLOPT_HTTPHEADER,$request_header);
curl_setopt($ch,CURLOPT_URL,$request_url);
curl_setopt($ch,CURLOPT_POSTFIELDS,$request_body);
curl_setopt($ch,CURLOPT_CUSTOMREQUEST,'POST');
$result = curl_exec($ch);
var_dump($ch);
var_dump($result);
curl_close($ch);
exit;
}
折腾2天了,都是返回:HTTP/1.1 403 Forbidden Server: Tengine Date: Tue, 18 Oct 2016 07:29:07 GMT Content-Length: 0 Connection: keep-alive resource(9) of type (curl) bool(true)。
也不知道是哪里的问题,亲熟悉ONS的大牛给看看。。
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。