Skip to content

Instantly share code, notes, and snippets.

@BigLobsterito
Created May 1, 2019 18:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save BigLobsterito/6de7ca92d4a6eea79c7fb5cfaa6cd5f9 to your computer and use it in GitHub Desktop.
Save BigLobsterito/6de7ca92d4a6eea79c7fb5cfaa6cd5f9 to your computer and use it in GitHub Desktop.

How to Detect Language for a String in PHP

Installation

To install unirest-php with Composer, just add the following to your composer.json file:

{
    "require-dev": {
        "mashape/unirest-php": "3.*"
    }
}

or by running the following command:

composer require mashape/unirest-php

Usage

Composer installs autoloader at ./vendor/autoloader.php. to include the library in your script, add:

require_once 'vendor/autoload.php';

So you're probably wondering how using Unirest makes creating requests in PHP easier, let's look at a working example:

$apiKey = "key_from_rapidapi";
$text_for_detection = "Some text needed to detect its language";

$response = Unirest\Request::post("https://language-identification-prediction.p.rapidapi.com/v1/recognize-language/",
  array(
    "X-RapidAPI-Host" => "language-identification-prediction.p.rapidapi.com",
    "X-RapidAPI-Key" => $apiKey,
    "Content-Type" => "application/x-www-form-urlencoded"
  ),
  array(
    "text" => $text_for_detection
  )
);

// full description of response here: https://rapidapi.com/BigLobster/api/language-identification-prediction/details
var_dump($response);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment