Home  >  Article  >  Backend Development  >  Use PHP to write an example tutorial for docking Baidu Machine Translation API

Use PHP to write an example tutorial for docking Baidu Machine Translation API

PHPz
PHPzOriginal
2023-08-13 11:13:46791browse

Use PHP to write an example tutorial for docking Baidu Machine Translation API

Using PHP to write an example tutorial for docking Baidu Machine Translation API

Introduction:
As people's demand for language communication continues to increase, machine translation technology has gradually become Today's hot technologies. The Baidu Machine Translation API provides developers with an easy way to integrate machine translation functions into their own applications. This article will introduce how to use PHP to write an example tutorial for docking Baidu Machine Translation API, and attach corresponding code examples.

  1. Register a Baidu developer account and create a machine translation application
    To use the Baidu machine translation API, you first need to register a Baidu developer account. After registration is completed, enter the Baidu Developer Console, create a machine translation application, and obtain the API Key and Secret Key of the application. These will be the credentials we need when calling the API in the code.
  2. Install and configure the curl extension of PHP
    Before using PHP to call Baidu Machine Translation API, you need to ensure that your PHP environment has the curl extension installed. Turn on the curl extension in the php.ini file and restart the web server.
  3. Write PHP code to interact with Baidu Machine Translation API
    Create a file named "BaiduTranslationAPI.php" and write the following code in the file:
<?php

class BaiduTranslationAPI {
    private $api_key;
    private $secret_key;
    
    public function __construct($api_key, $secret_key) {
        $this->api_key = $api_key;
        $this->secret_key = $secret_key;
    }
    
    public function translate($text, $from = 'auto', $to = 'auto') {
        $url = 'https://fanyi-api.baidu.com/api/trans/vip/translate';
        $salt = rand(10000, 99999);
        $sign = md5($this->api_key . $text . $salt . $this->secret_key);
        
        $params = [
            'q' => $text,
            'from' => $from,
            'to' => $to,
            'appid' => $this->api_key,
            'salt' => $salt,
            'sign' => $sign
        ];
        
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($params));
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
        
        $response = curl_exec($ch);
        curl_close($ch);
        
        $result = json_decode($response, true);
        
        return $result['trans_result'][0]['dst'];
    }
}
?>
  1. Use Baidu Machine Translation API for translation
    In your PHP application, you can create an instance of BaiduTranslationAPI and call the translate method to translate.
<?php
require_once 'BaiduTranslationAPI.php';

$api_key = '你的API Key';
$secret_key = '你的Secret Key';

$translator = new BaiduTranslationAPI($api_key, $secret_key);

$text = 'Hello, World!';
$source_language = 'en';
$target_language = 'zh';

$translated_text = $translator->translate($text, $source_language, $target_language);

echo "原文:" . $text . "
";
echo "译文:" . $translated_text . "
";
?>

The above code will output the original text and translation, and print the result.

Conclusion:
Through the introduction of this article, you have learned how to use PHP to write an example tutorial for docking Baidu Machine Translation API. You can call different Baidu machine translation API interfaces according to your own needs to achieve more functions. I hope this tutorial can help you successfully integrate Baidu Machine Translation API into your application.

The above is the detailed content of Use PHP to write an example tutorial for docking Baidu Machine Translation API. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn