PHP Classes
elePHPant
Icontem

File: examples/lookup.php

Recommend this page to a friend!
  Classes of Sergey Karavay  >  PHPolyglot  >  examples/lookup.php  >  Download  
File: examples/lookup.php
Role: Example script
Content type: text/plain
Description: Example script
Class: PHPolyglot
Translate, correct spelling and speak a given text
Author: By
Last change: Updates:
- fixed some minor issues.
Date: 3 months ago
Size: 881 bytes
 

Contents

Class file image Download
<?php

require_once __DIR__ . '/../vendor/autoload.php';

use
GinoPane\PHPolyglot\PHPolyglot;

try {
   
$phpolyglot = new PHPolyglot();

   
$textToLookup = 'Hello!';

   
$languageFrom = 'en';

   
$response = $phpolyglot->lookup($textToLookup, $languageFrom)->getEntries();

    if (empty(
$response)) {
        throw new
Exception('Nothing returned! Maybe API has changed?');
    }

   
$synonyms = implode(", ", $response[0]->getSynonyms());

   
$output = <<<TEXT
Initial word: {$response[0]->getTextFrom()}
 
    Part of speech:
{$response[0]->getPosFrom()}
    Transcription:
{$response[0]->getTranscription()}
   
    Main alternative:
{$response[0]->getTextTo()}
    Synonyms:
{$synonyms}

TEXT;

    echo
$output;
} catch (
Exception $exception) {
   
$errorMessage = $exception->getMessage();

    echo
sprintf("Error happened: %s", $errorMessage);
}

echo
PHP_EOL;