php使用百度翻譯api樣本分享_PHP教程

來源:互聯網
上載者:User
百度翻譯API的PHP代碼,測試可以實現,不過英譯中可能需要轉換編碼。
複製代碼 代碼如下:
function language($value,$from="auto",$to="auto")
{
$value_code=urlencode($value);
#首先對要翻譯的文字進行 urlencode 處理
$appid="YourApiKey";
#您註冊的API Key
$languageurl = "http://openapi.baidu.com/public/2.0/bmt/translate?client_id=" . $appid ."&q=" .$value_code. "&from=".$from."&to=".$to;
#產生翻譯API的URL GET地址
$text=json_decode(language_text($languageurl));
$text = $text->trans_result;
return $text[0]->dst;
}
function language_text($url) #擷取目標URL所列印的內容
{
if(!function_exists('file_get_contents')) {
$file_contents = file_get_contents($url);
} else {
$ch = curl_init();
$timeout = 5;
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$file_contents = curl_exec($ch);
curl_close($ch);
}
return $file_contents;
}
echo language('中國');
?>

http://www.bkjia.com/PHPjc/728090.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/728090.htmlTechArticle百度翻譯API的PHP代碼,測試可以實現,不過英譯中可能需要轉換編碼。 複製代碼 代碼如下: ?php function language($value,$from="auto",$to="auto") { $v...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.