×

Welcome to TagMyCode

Please login or create account to add a snippet.
0
0
 
0
Language: PHP
Posted by: r52snip
Added: Sep 19, 2018 5:52 AM
Views: 3394
  1. define('YANDEX_API', 'KEY');
  2.  
  3. function loadData($url, $ref = false) {
  4.     $chImg = curl_init($url);
  5.     curl_setopt($chImg, CURLOPT_RETURNTRANSFER, true);
  6.     curl_setopt($chImg, CURLOPT_USERAGENT, "Mozilla/5.0 (Macintosh; Intel Mac OS X 10.6; rv:2.0) Gecko/20100101 Firefox/4.0");
  7.     if ($ref) {
  8.         curl_setopt($chImg, CURLOPT_REFERER, $ref);
  9.     }
  10.     $curl_scraped_data = curl_exec($chImg);
  11.     curl_close($chImg);
  12.     return $curl_scraped_data;
  13. }
  14.  
  15.  
  16.  
  17. function translate($text, $from = 'ru', $to = 'en') {
  18.     $data = loadData('https://translate.yandex.net/api/v1.5/tr.json/translate?key=' . YANDEX_API . '&lang='.$from.'-'.$to.'&text=' . urlencode($text));
  19.     $translated = json_decode($data);
  20.     if (sizeof($translated) > 0) {
  21.         if (isset($translated->text[0])) {
  22.             return $translated->text[0];
  23.         } else {
  24.             return false;
  25.         }
  26.     } else {
  27.         return false;
  28.     }
  29. }