java代码
详见阿里巴巴录音转文字代码;
php代码
原php验证签名有误::
首先json数据和java的json数据完全不一致>>>
解决方法:将java中的json数据打印出来 得到ascii码
<?php
$arr = [
"app_key"=>"nls-service-telephone8khz",
"oss_link"=>'http://aliyun-nls.oss.aliyuncs.com/asr/fileASR/examples/nls-sample.wav'
];
//echo $body;
//要命的构造
$body = '{'.chr(10).chr(9).'"app_key":"nls-service-telephone8khz",'.chr(10).chr(9).'"oss_link":"http://aliyun-nls.oss.aliyuncs.com/asr/fileASR/examples/nls-sample.wav"'.chr(10).'}';
//$body = '{"app_key":"nls-service-telephone8khz","oss_link":"http://aliyun-nls.oss.aliyuncs.com/asr/fileASR/examples/nls-sample.wav"}';
//$body = "{\"app_key\":\"nls-service-telephone8khz\",\"oss_link\":\"http://aliyun-nls.oss.aliyuncs.com/asr/fileASR/examples/nls-sample.wav\"}";
//echo base64_encode(md5($body,true));
$akId = "LTAIXRo6*****";
$akSecret = "LeoA5vwdnN******";
//
//更新api信息
$url = "https://nlsapi.aliyun.com/transcriptions";
//找不到数据
$arr = [
"app_key"=>"nls-service-telephone8khz",
"oss_link"=>'http://aliyun-nls.oss.aliyuncs.com/asr/fileASR/examples/nls-sample.wav'
];
$options = array(
'http' => array(
'header' => array(
'accept'=> "application/json",
'content-type'=> "application/json",
'date'=> gmdate("D, d M Y H:i:s \G\M\T"),
'authorization' => ''
),
'method' => "POST", //可以是 GET, POST, DELETE, PUT
'content' => $body //如有数据,请用json_encode()进行编码
)
);
$http = $options['http'];
$header = $http['header'];
$urlObj = parse_url($url);
if(empty($urlObj["query"]))
$path = $urlObj["path"];
else
$path = $urlObj["path"]."?".$urlObj["query"];
$body = $http['content'];
if(empty($body))
$bodymd5 = $body;
else
$bodymd5 = base64_encode(md5($body,true));
//echo $bodymd5;
//这里也要改
$stringToSign = $http['method']."\n".$header['accept']."\n".$bodymd5."\n".$header['content-type']."\n".$header['date'];
$signature = base64_encode(hash_hmac("sha1",$stringToSign,$akSecret, true));
$authHeader = "Dataplus "."$akId".":"."$signature";
$options['http']['header']['authorization'] = $authHeader;
$options['http']['header'] = implode(
array_map(
function($key, $val){
return $key.":".$val."\r\n";
},
array_keys($options['http']['header']),
$options['http']['header']));
$context = stream_context_create($options);
$file = file_get_contents($url, false, $context );
echo($file);
/*
* {
"app_key": "***",
"oss_link": "http://aliyun-nls.oss.aliyuncs.com/asr/fileASR/examples/nls-sample.wav",
"auto_split":false,
"vocabulary_id":"vocab_id",
//valid_times是获取语音的指定时间段的识别内容,如果不需要,则不用填写
"valid_times": [
{
"begin_time": 200,
"end_time":2000,
"channel_id": 0
}
]
}
*/
/*
开始的错误 就是签名有误造成的
{"id":"3cd6e3d9c2884598b9aa3a38c03d7953","request_id":"3cd6e3d9c2884598b9aa3a38c03d7953","
* error_code":80103,"error_message":"The authorization(Dataplus LTAIq9ZYotEshUP7:mN4iM8gk1q7ox6e7FVv3fINegZc=)
* of this request Invalid! {\"url\":\"/?accessKey=LTAIq9ZYotEshUP7&serviceCode=nls&api=authCheck3&token=12e8a7067e5977038095df899f016712\",
* \"headers\":{\"accept\":\"application/json\",\"date\":\"Fri, 02 Mar 2018 01:50:56 GMT\",\"content-type\":\"application/json\",\"authorization\":\"Dataplus LTAIq9ZYotEshUP7:mN4iM8gk1q7ox6e7FVv3fINegZc=\"},
* \"method\":\"POST\"}
{\"url\":\"/?accessKey=LTAIq9ZYotEshUP7&serviceCode=nls&api=authCheck3&token=12e8a7067e5977038095df899f016712\",\"headers\":{\"accept\":\"application/json\",\"date\":\"Fri, 02 Mar 2018 01:50:56 GMT\",\"content-type\":\"application/json\",\"authorization\":\"Dataplus LTAIq9ZYotEshUP7:mN4iM8gk1q7ox6e7FVv3fINegZc=\"},\"method\":\"POST\"}"}
*/
网友评论