How to Build a Simple Online Translation Tool with PHP

This article guides readers through creating a simple online translation feature using PHP, covering prerequisite steps such as obtaining a translation API key, writing the core translation function, handling user input via an HTML form, and testing the implementation.

php Courses
php Courses
php Courses
How to Build a Simple Online Translation Tool with PHP

Preparation

Before writing code, you need to create a translation API account (e.g., Google Translate API or Microsoft Translator API) and obtain an API key, then create a new PHP file to hold the translation logic.

Create a translation API account

Select an API provider, register, and get the API key.

Create a new PHP file

This file will contain the PHP code for the translator.

Writing the code

The following steps show how to implement a simple online translation feature in PHP.

Include the API key

$apiKey = 'YOUR_API_KEY';

Replace YOUR_API_KEY with your actual key.

Create a translation function

function translateText($text, $source, $target) {
    // Build request URL
    $url = 'https://translation.googleapis.com/language/translate/v2?key=' . $apiKey;
    $url .= '&q=' . rawurlencode($text);
    $url .= '&source=' . $source;
    $url .= '&target=' . $target;

    // Send GET request and get response
    $response = file_get_contents($url);

    // Decode JSON response
    $data = json_decode($response);

    // Return translated text
    return $data->data->translations[0]->translatedText;
}

This example uses Google Translate API; you can adapt it to other services.

Handle user input

Process the form submission and call the translation function.

if(isset($_POST['submit'])) {
    $text = $_POST['text'];
    $source = $_POST['source'];
    $target = $_POST['target'];

    $translatedText = translateText($text, $source, $target);
    echo "翻译结果:" . $translatedText;
}

Testing

After completing the code, open the PHP file in a browser, input the text, source language, and target language, and verify that the translation result is displayed.

Creating a simple online translator with PHP is not complex; for more advanced features you may need to study additional API options and techniques.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Web DevelopmentPHPGoogle TranslateTranslation API
php Courses
Written by

php Courses

php中文网's platform for the latest courses and technical articles, helping PHP learners advance quickly.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.