├── README.md ├── LICENSE.txt ├── Languages.php ├── Detect.php ├── BreakSentence.php ├── Translate.php ├── DictionaryLookup.php ├── DictionaryExamples.php └── Transliterate.php /README.md: -------------------------------------------------------------------------------- 1 | # Text-Translation-API-V3-PHP 2 | 3 | ## Prerequisites 4 | You will need PHP 5.6.x to run this code. 5 | 6 | You must have a Cognitive Services API account with Microsoft Translator Text API. You will need a paid subscription key from your Azure dashboard. 7 | 8 | 9 | ## Code samples 10 | The code in this repository contains examples for all of the Microsoft Text Translator V3 API methods. Each file demonstrates a single method. To try out a method: 11 | 12 | #### Create a new PHP project in your favorite IDE. 13 | #### Add the provided code. 14 | #### Replace the key value with an access key valid for your subscription. 15 | #### Run the program. 16 | 17 | [Translator Text API Reference](https://docs.microsoft.com/en-us/azure/cognitive-services/translator/) 18 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) Microsoft Corporation. All rights reserved. 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE 22 | 23 | Third Party Programs: The software may include third party programs that Microsoft, 24 | not the third party, licenses to you under this agreement. Notices, if any, for the 25 | third party programs are included for your information only. 26 | -------------------------------------------------------------------------------- /Languages.php: -------------------------------------------------------------------------------- 1 | array ( 22 | 'header' => $headers, 23 | 'method' => 'GET' 24 | ) 25 | ); 26 | $context = stream_context_create ($options); 27 | $result = file_get_contents ($host . $path, false, $context); 28 | return $result; 29 | } 30 | $result = GetLanguages ($endpoint, $path); 31 | // Note: We convert result, which is JSON, to and from an object so we can pretty-print it. 32 | // We want to avoid escaping any Unicode characters that result contains. See: 33 | // http://php.net/manual/en/function.json-encode.php 34 | $json = json_encode(json_decode($result), JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); 35 | // Write the output to file. 36 | $out = fopen($output_path, 'w'); 37 | fwrite($out, $json); 38 | fclose($out); 39 | ?> 40 | -------------------------------------------------------------------------------- /Detect.php: -------------------------------------------------------------------------------- 1 | array ( 46 | 'header' => $headers, 47 | 'method' => 'POST', 48 | 'content' => $content 49 | ) 50 | ); 51 | $context = stream_context_create ($options); 52 | $result = file_get_contents ($host . $path . $params, false, $context); 53 | return $result; 54 | } 55 | 56 | $requestBody = array ( 57 | array ( 58 | 'Text' => $text, 59 | ), 60 | ); 61 | $content = json_encode($requestBody); 62 | 63 | $result = DictionaryLookup ($endpoint, $path, $subscription_key, "", $content); 64 | 65 | // Note: We convert result, which is JSON, to and from an object so we can pretty-print it. 66 | // We want to avoid escaping any Unicode characters that result contains. See: 67 | // http://php.net/manual/en/function.json-encode.php 68 | $json = json_encode(json_decode($result), JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); 69 | echo $json; 70 | ?> 71 | -------------------------------------------------------------------------------- /BreakSentence.php: -------------------------------------------------------------------------------- 1 | array ( 46 | 'header' => $headers, 47 | 'method' => 'POST', 48 | 'content' => $content 49 | ) 50 | ); 51 | $context = stream_context_create ($options); 52 | $result = file_get_contents ($host . $path . $params, false, $context); 53 | return $result; 54 | } 55 | 56 | $requestBody = array ( 57 | array ( 58 | 'Text' => $text, 59 | ), 60 | ); 61 | $content = json_encode($requestBody); 62 | 63 | $result = BreakSentences ($endpoint, $path, $subscription_key, "", $content); 64 | 65 | // Note: We convert result, which is JSON, to and from an object so we can pretty-print it. 66 | // We want to avoid escaping any Unicode characters that result contains. See: 67 | // http://php.net/manual/en/function.json-encode.php 68 | $json = json_encode(json_decode($result), JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); 69 | echo $json; 70 | ?> 71 | -------------------------------------------------------------------------------- /Translate.php: -------------------------------------------------------------------------------- 1 | array ( 49 | 'header' => $headers, 50 | 'method' => 'POST', 51 | 'content' => $content 52 | ) 53 | ); 54 | $context = stream_context_create ($options); 55 | $result = file_get_contents ($host . $path . $params, false, $context); 56 | return $result; 57 | } 58 | 59 | $requestBody = array ( 60 | array ( 61 | 'Text' => $text, 62 | ), 63 | ); 64 | $content = json_encode($requestBody); 65 | 66 | $result = Translate ($endpoint, $path, $subscription_key, $params, $content); 67 | 68 | // Note: We convert result, which is JSON, to and from an object so we can pretty-print it. 69 | // We want to avoid escaping any Unicode characters that result contains. See: 70 | // http://php.net/manual/en/function.json-encode.php 71 | $json = json_encode(json_decode($result), JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); 72 | echo $json; 73 | ?> 74 | -------------------------------------------------------------------------------- /DictionaryLookup.php: -------------------------------------------------------------------------------- 1 | array ( 49 | 'header' => $headers, 50 | 'method' => 'POST', 51 | 'content' => $content 52 | ) 53 | ); 54 | $context = stream_context_create ($options); 55 | $result = file_get_contents ($host . $path . $params, false, $context); 56 | return $result; 57 | } 58 | 59 | $requestBody = array ( 60 | array ( 61 | 'Text' => $text, 62 | ), 63 | ); 64 | $content = json_encode($requestBody); 65 | 66 | $result = DictionaryLookup ($endpoint, $path, $subscription_key, $params, $content); 67 | 68 | // Note: We convert result, which is JSON, to and from an object so we can pretty-print it. 69 | // We want to avoid escaping any Unicode characters that result contains. See: 70 | // http://php.net/manual/en/function.json-encode.php 71 | $json = json_encode(json_decode($result), JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); 72 | echo $json; 73 | ?> 74 | -------------------------------------------------------------------------------- /DictionaryExamples.php: -------------------------------------------------------------------------------- 1 | array ( 50 | 'header' => $headers, 51 | 'method' => 'POST', 52 | 'content' => $content 53 | ) 54 | ); 55 | $context = stream_context_create ($options); 56 | $result = file_get_contents ($host . $path . $params, false, $context); 57 | return $result; 58 | } 59 | 60 | $requestBody = array ( 61 | array ( 62 | 'Text' => $text, 63 | 'Translation' => $translation, 64 | ), 65 | ); 66 | $content = json_encode($requestBody); 67 | 68 | $result = DictionaryExamples ($endpoint, $path, $subscription_key, $params, $content); 69 | 70 | // Note: We convert result, which is JSON, to and from an object so we can pretty-print it. 71 | // We want to avoid escaping any Unicode characters that result contains. See: 72 | // http://php.net/manual/en/function.json-encode.php 73 | $json = json_encode(json_decode($result), JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); 74 | echo $json; 75 | ?> 76 | -------------------------------------------------------------------------------- /Transliterate.php: -------------------------------------------------------------------------------- 1 | array ( 50 | 'header' => $headers, 51 | 'method' => 'POST', 52 | 'content' => $content 53 | ) 54 | ); 55 | $context = stream_context_create ($options); 56 | $result = file_get_contents ($host . $path . $params, false, $context); 57 | return $result; 58 | } 59 | 60 | $requestBody = array ( 61 | array ( 62 | 'Text' => $text, 63 | ), 64 | ); 65 | $content = json_encode($requestBody); 66 | 67 | $result = Transliterate ($endpoint, $path, $subscription_key, $params, $content); 68 | 69 | // Note: We convert result, which is JSON, to and from an object so we can pretty-print it. 70 | // We want to avoid escaping any Unicode characters that result contains. See: 71 | // http://php.net/manual/en/function.json-encode.php 72 | $json = json_encode(json_decode($result), JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT); 73 | echo $json; 74 | ?> 75 | --------------------------------------------------------------------------------