腾讯云翻译接口 PHP 示例代码 - 中英文翻译实现
'orders' => '订单', 'All' => '所有', 'pending' => '待处理', 'confirmed' => '已确认', 'Processing' => '处理中', 'out_for_delivery' => '外发递送', 'delivered' => '已送达',
// 腾讯云翻译接口示例代码 function translate($text, $source, $target) { $api_url = 'https://api.ai.qq.com/fcgi-bin/nlp/nlp_texttranslate'; $app_key = 'Your App Key'; $app_id = 'Your App ID'; $nonce_str = uniqid(); $time_stamp = time(); $sign = md5('app_id='.$app_id.'&app_key='.$app_key.'&nonce_str='.$nonce_str.'&source='.$source.'&target='.$target.'&text='.$text.'&time_stamp='.$time_stamp);
$post_data = array(
'app_id' => $app_id,
'time_stamp' => $time_stamp,
'nonce_str' => $nonce_str,
'sign' => $sign,
'text' => $text,
'source' => $source,
'target' => $target
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $api_url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data));
$response = curl_exec($ch);
curl_close($ch);
$result = json_decode($response, true);
if ($result['ret'] == 0) {
return $result['data']['target_text'];
} else {
return $result['msg'];
}
}
// 使用示例 $text = 'orders'; $source = 'en'; $target = 'zh'; $result = translate($text, $source, $target); echo $result; // 输出:订单
原文地址: https://www.cveoy.top/t/topic/oQYn 著作权归作者所有。请勿转载和采集!