java代碼
詳見阿里巴巴錄音轉文字代碼;
php代碼
原php驗證簽名有誤::
首先json數(shù)據(jù)和java的json數(shù)據(jù)完全不一致>>>
解決方法:將java中的json數(shù)據(jù)打印出來 得到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";
//找不到數(shù)據(jù)
$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 //如有數(shù)據(jù),請用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\"}"}
*/