<?php
/**
 * @desc 百度文档翻译服务接口 
 *       计数服务接口校验同理，完整文档参考：https://api.fanyi.baidu.com/doc/27
 * @date 2020-11-10
 * @author @baidu.com
 */

$params = array(
    'appid' => '', //你的appid
    'from' => 'zh', 
    'to' => 'en',
    'timestamp' => time(), //10位时间戳
    'type' => 'txt', //文档类型，根据具体文档而定
);
$seckey = ''; //你的密钥
ksort($params);
$querySign = '';
foreach ($params as $key => $value) {
    $querySign .= $key . '=' . $value . '&';
}
$filePath = ''; //文档路径
$params['sign'] = md5($querySign . '' . md5_file($filePath) . '' . $seckey);

$url = 'https://fanyi-api.baidu.com/api/trans/vip/doccount';
$header = array(
    'Content-Type' => 'multipart/form-data',
       );

$params['file'] =  '@' . realpath($filePath);
//高版本兼容
if (class_exists('\CURLFile')) {
    $params['file'] = new \CURLFile(realpath($filePath));
}

$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,$params);
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_TIMEOUT, 10);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result=curl_exec($ch);
curl_close($ch);
$callRet = json_decode($result, true);
print_R($result); 


?>


