PHP uses the Baidu translation API sample to share _php instances

Source: Internet
Author: User
Baidu Translation API PHP code, testing can be achieved, but English translation may need to convert the encoding.
Copy CodeThe code is as follows:
function language ($value, $from = "Auto", $to = "Auto")
{
$value _code=urlencode ($value);
#首先对要翻译的文字进行 UrlEncode Processing
$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 Address
$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 (' China ');
?>
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.