科大讯飞机器翻译 API 接口PHP接入

本文借助科大讯飞开放平台,以机器翻译为例构建一个API应用。官方提供的demo有python3、java、nodejs、php、go语言,这里使用PHP。

还没有使用讯飞星火的朋友可以参考此文(附机器翻译API源码)

讯飞星火大模型API矩阵

官方文档:https://www.xfyun.cn/doc/nlp/xftrans/API.html

<?php
/**
* 机器翻译 WebAPI 接口调用示例
* 运行前:请先填写Appid、APIKey、APISecret
*
* 1.接口文档(必看):https://www.xfyun.cn/doc/nlp/xftrans/API.html
* 2.错误码链接:https://www.xfyun.cn/document/error-code (错误码code为5位数字)
* @author iflytek
*/
class Its_test {
function tocurl($url, $header, $content){
$ch = curl_init();
if(substr($url,0,5)=='https'){
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 跳过证书检查
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); // 从证书中检查SSL加密算法是否存在
curl_setopt($ch, CURLOPT_SSLVERSION, 1);
}
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
if (is_array($header)) {
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
}
curl_setopt($ch, CURLOPT_POST, true);
if (!empty($content)) {
if (is_array($content)) {
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($content));
} else if (is_string($content)) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $content);
}
}
$response = curl_exec($ch);
$error=curl_error($ch);
//var_dump($error);
if($error){
die($error);
}
$header = curl_getinfo($ch);
curl_close($ch);
$data = array('header' => $header,'body' => $response);
return $data;
}
function xfyun($text,$from,$to) {
//在控制台-我的应用-机器翻译获取
$app_id = "XXXXXXXXX";
//在控制台-我的应用-机器翻译获取
$api_sec = "XXXXXXXXX";
//在控制台-我的应用-机器翻译获取
$api_key = "XXXXXXXXX";
// 机器翻译接口地址
$url = "https://itrans.xfyun.cn/v2/its";
//body组装
//$text = "中华人民共和国于1949年成立";//待翻译文本
$body = json_encode($this->getBody($app_id, $from,$to, $text));
// 组装http请求头
$date =gmdate('D, d M Y H:i:s') . ' GMT';
$digestBase64 = "SHA-256=".base64_encode(hash("sha256", $body, true));
$builder = sprintf("host: %s
date: %s
POST /v2/its HTTP/1.1
digest: %s"
,
"itrans.xfyun.cn"
, $date, $digestBase64);
// echo($builder);
$sha = base64_encode(hash_hmac("sha256", $builder, $api_sec, true));
$authorization = sprintf("api_key=\"%s\", algorithm=\"%s\", headers=\"%s\", signature=\"%s\"", $api_key,"hmac-sha256",
"host date request-line digest", $sha);
$header = [
"Authorization: ".$authorization,
'Content-Type: application/json',
'Accept: application/json,version=1.0',
'Host: itrans.xfyun.cn',
'Date: ' .$date,
'Digest: '.$digestBase64
];
$response = $this->tocurl($url, $header, $body);
// var_dump($response['body']);
$res = json_decode($response['body'], true);//转换为数组
header("content-type: application/json");
$json_return = array(
"code" => "200",
"src" => $text,
"dst" => $res['data']['result']['trans_result']['dst']
);
echo json_encode($json_return, JSON_UNESCAPED_UNICODE);
}
function getBody($app_id, $from, $to, $text) {
$common_param = [
'app_id' => $app_id
];
$business = [
'from' => $from,
'to' => $to,
];
$data = [
"text" => base64_encode($text)
];
return $body = [
'common' => $common_param,
'business' => $business,
'data' => $data
];
}
}
header('Access-Control-Allow-Origin:*');
header('Content-type: application/json');
$text=isset($_GET['text'])? $_GET['text'] :null;
if(empty($text)){die("请传入text参数");}
$from=isset($_GET['from'])? $_GET['from'] :null;
if(empty($from)){die("请传入源语言from参数");}
$to=isset($_GET['to'])? $_GET['to'] :null;
if(empty($to)){
if (preg_match("/([\x81-\xfe][\x40-\xfe])/", $text, $match)) {
//含有汉字
$to = 'en';
} else {
//不含有汉字
$to = 'cn';
}
}
$a = new Its_test();
$a->xfyun($text,$from,$to);

请求地址​:

https:/你的api地址/?text=hello,world&from=en&to=cn

返回​数据:

{
"code": "200",
"src": "hello,world",
"dst": "你好,世界"
}

Demo:https://code.szfx.top/api/xftrans.html

本文采用 CC BY-NC-SA 3.0 Unported 许可,转载请以超链接注明出处。
原文地址:科大讯飞机器翻译 API 接口PHP接入 作者:松鼠小
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
Source: Telegram @AmashiroNatsukiEars_NoWord Sticker
Source: Github @zhheo/Sticker-Heo
Source: github.com/k4yt3x/flowerhd
颜文字
AmashiroNatsukiEars
Heo
小恐龙
花!
上一篇
下一篇
  1. 1 Rain after Summer 羽肿
  2. 2 潮鳴り 折戸伸治
  3. 3 Inspire Capo Productions
  4. 4 Hyacinth July
  5. 5 My Soul July
  6. 6 Best Moments (feat. Kondor) Blazo/Kondor
  7. 7 Lost Love (Instrumental) Lunnna/Janey杰尼
  8. 8 风居住的街道(Piano ver) (翻自 磯村由紀子) 饭碗的彼岸
  9. 9 Beyond The Memory July
  10. 10 メインテーマ「永遠の一瞬」 伊藤賢治
  11. 11 Journey Capo Productions
  12. 12 Flower Dance DJ Okawari
  13. 13 Righteous Path Blazo
  14. 14 いのちの名前 広橋真紀子
  15. 15 花火が瞬く夜に 羽肿
  16. 16 记得 风潮音乐
  17. 17 Encouragement Kondor
  18. 18 Rise - Epic Music John Dreamer
  19. 19 A Little Story Valentin
  20. 20 City 羽肿
  21. 21 《夜色钢琴曲》kiss the rain(翻自 YIRUMA) 赵海洋
  22. 22 Moon Flow Nomak
  23. 23 River Flows in You Yiruma
  24. 24 白昼之夜 林隆璇
  25. 25 LOVIN' YOU 押尾コータロー
  26. 26 ALONE ON THE WAY Pianoboy高至豪
  27. 27 Intro The xx
  28. 28 Somewhere July
  29. 29 我在那一角落患过伤风(钢琴版) 群星
  30. 30 Rainy Day Reminiscence Soulostar
  31. 31 Refrain 阿南亮子
  32. 32 STEPPING ON THE RAINY STREET 群星
  33. 33 Angel and Devil Elyonbeats
  34. 34 Butterfly Waltz Brian Crain
  35. 35 Mallow Flower Otokaze
  36. 36 The truth that you leave Pianoboy高至豪
  37. 37 canon in d Brian Crain
  38. 38 めぐる季節 久石譲/奥户巴寿
  39. 39 拥有 几米
  40. 40 碎月雨中奏 iw ix
  41. 41 Always With Me 木村弓/奥户巴寿
  42. 42 Everlasting Truth (Album Mix) re:plus
  43. 43 The promise Secret Garden
  44. 44 Luv Letter DJ Okawari
Rain after Summer - 羽肿
00:00 / 00:00
An audio error has occurred, player will skip forward in 2 seconds.

Loading