Download the PHP package potsky/microsoft-translator-php-sdk without Composer

On this page you can find all versions of the php package potsky/microsoft-translator-php-sdk. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package microsoft-translator-php-sdk

PHP SDK for Microsoft Translator

Latest Stable Version Total Downloads Build Status Coverage Status

Table of content

  1. Installation
  2. Configuration
  3. Usage
  4. Change Log
  5. Contribute

1. Installation

Requirements

The SDK needs the cURL PHP extension.

With composer

Install the latest stable version using composer: composer require potsky/microsoft-translator-php-sdk

Or add potsky/microsoft-translator-php-sdk to your composer.json :

Manually

Is it time to move to composer?

Not now? Ok, just include src/MicrosoftTranslator.php in your PHP script :

2. Configuration

2.1 Microsoft Account

  1. Sign up for a Microsoft Azure account (credit card required)

    If you don’t already have an Azure account, sign up for a Microsoft Azure account at http://azure.com.

  2. After you have created an Azure account, subscribe to Azure. You will not be charged for Azure unless you use it. Then sign into the Azure portal at http://portal.azure.com.

  3. Add a Microsoft Translator API subscription to your Azure account.

    • Select the + New option
    • Select AI + Cognitive Services from the list of Azure services
    • Select either Translator Text API
    • Press the Create button when you’re ready to create your Microsoft Translator API subscription.
    • Complete the fields and press the Create button and you’re now subscribed to Microsoft Translator.
  4. Retrieve your authentication key.

    • Go to All Resources and select the Microsoft Translator API subscription account you are subscribed to.
    • Select the Keys option and copy the first subscription key to access the service.

2.2 SDK

When instantiating a new client, you pass an array of configuration parameters:

Here is the list of available parameters, some are mandatory:

Name Default Description
api_client_key mandatory Your subscription key 1
api_access_token - You can directly give an access token. In this case api_client_key will not be used but this token longs only 10 minutes according to the microsoft documentation
api_base_url http://api.microsofttranslator.com/V2/Http.svc/
auth_base_url https://datamarket.accesscontrol.windows.net/v2/OAuth2-13/
guard_type file The only available type is file but you can specify your own Guard by setting the classname like this YourNameSpace\\YourGuardWhichImplementsTheMicrosoftTranslatorGuardInterface
guard_file_dir_path The default PHP tmp directory You can specify a custom directory. IT MUST NOT BE EXPOSED TO INTERNET given that clear access token will be stored in files...
log_level No log To enable log, choose the maximum severity you want to log in this list : \MicrosoftTranslator\Logger::LEVEL_DEBUG, \MicrosoftTranslator\Logger::LEVEL_INFO, \MicrosoftTranslator\Logger::LEVEL_WARNING, \MicrosoftTranslator\Logger::LEVEL_ERROR and \MicrosoftTranslator\Logger::LEVEL_FATAL
log_file_path No file path, output with error_log() function Set a file path to log in this file
http_timeout 10 The timeout in seconds for requests
http_proxy_host - The proxy host if you need a proxy for outgoing connections to the API
http_proxy_type URLPROXY_HTTP One of these values: URLPROXY_HTTP, CURLPROXY_SOCKS4, CURLPROXY_SOCKS5
http_proxy_auth CURLAUTH_BASIC One if these values: CURLAUTH_BASIC, CURLAUTH_NTLM
http_proxy_port 3128 The proxy port
http_proxy_user - The proxy user name if your proxy needs authentication
http_proxy_pass - The proxy user password if your proxy needs authentication
http_user_agent MicrosoftTranslator PHP SDK v%VERSION% You can use your own user agent and you can use the placeholder %VERSION% to inject the current SDK version

3. Usage

3.1 Methods

Define a client with your credentials:

Translate a text

Translate word chair in german. The API will try to guess the input language:

Result:

Translate word chair in german by specifying the initial sentence is in french:

Result:

Translate an array of texts

Result:

You can specify an input language as usual.

Transform a text

Only english is supported.

Result:

Detect the language of a text

Result:

Detect the language of an array of texts

Result:

Get language names

Result:

Get available languages for translation

Result:

Break sentences

Result:

3.2 Handle errors

SDK can throw an MicrosoftTranslator\Exception in several cases :

You should catch these errors like this:

or like this by checking the returned code instead:

3.3 Customize the SDK

You can use your own classes to implement several parts of the SDK. Your classes need to implement interfaces and they will get the configuration array in the constructors. You can then customize your classes at runtime.

3.3.1 Inject a new Logger

You can use your own Logger class. The Logger class is used to log SDK messages.

It must implement the \MicrosoftTranslator\LoggerInterface interface.

Then use it when instantiating a client:

3.3.2 Inject a new HTTP manager

You can use your own Http class. The Http class manages the HTTP connexions to reach the accumulator API.

It must implement the \MicrosoftTranslator\HttpInterface interface.

Then use it when instantiating a client:

3.3.3 Inject a new Auth manager

You can use your own Auth class. The Auth class responsibility is to get an access token and to give it to the Guard manager.

It must implement the \MicrosoftTranslator\AuthInterface interface.

Then use it when instantiating a client:

3.3.4 Inject a new Guard manager

You can use your own Guard manager. The Guard class responsibility is to store access tokens and to manage expiration durations.

The Guard injection is a little bit different of others injections because it is done in the configuration array. This is a setter dependency injection instead of an instantiation dependency injection.

It must implement the \MicrosoftTranslator\GuardInterface interface.

Inject a new Guard manager via the configuration array:

3.4 Manage saved access tokens

The SDK maintains access tokens via a Guard object. The default Guard is a GuardFile which stores access tokens in files.

If you need to delete all saved access token, you can do this:

To delete only expired access tokens, run this:

4. Change Log

5. Contribute

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Added some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

Tests are in tests. To run the tests: vendor/bin/phpunit.

Coverage cannot decrease next a merge. To track file coverage, run vendor/bin/phpunit --coverage-html coverage and open coverage/index.html to check uncovered lines of code.


All versions of microsoft-translator-php-sdk with dependencies

PHP Build Version
Package Version
Requires ext-curl Version *
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package potsky/microsoft-translator-php-sdk contains the following files

Loading the files please wait ....