3v4l.org

run code in 300+ PHP versions simultaneously
<?php class GoogleTranslate { /** * Retrieves the translation of a text * * @param string $source * Original language of the text on notation xx. For example: es, en, it, fr... * @param string $target * Language to which you want to translate the text in format xx. For example: es, en, it, fr... * @param string $text * Text that you want to translate * * @return string a simple string with the translation of the text in the target language */ public static function translate($source, $target, $text) { // Request translation $response = self::requestTranslation($source, $target, $text); // Get translation text // $response = self::getStringBetween("onmouseout=\"this.style.backgroundColor='#fff'\">", "</span></div>", strval($response)); // Clean translation $translation = self::getSentencesFromJSON($response); return $translation; } /** * Internal function to make the request to the translator service * * @internal * * @param string $source * Original language taken from the 'translate' function * @param string $target * Target language taken from the ' translate' function * @param string $text * Text to translate taken from the 'translate' function * * @return object[] The response of the translation service in JSON format */ protected static function requestTranslation($source, $target, $text) { // Google translate URL $url = "https://translate.google.com/translate_a/single?client=at&dt=t&dt=ld&dt=qca&dt=rm&dt=bd&dj=1&hl=es-ES&ie=UTF-8&oe=UTF-8&inputm=2&otf=2&iid=1dd3b944-fa62-4b55-b330-74909a99969e"; $fields = array( 'sl' => urlencode($source), 'tl' => urlencode($target), 'q' => urlencode($text) ); if(strlen($fields['q'])>=5000) throw new \Exception("Maximum number of characters exceeded: 5000"); // URL-ify the data for the POST $fields_string = ""; foreach ($fields as $key => $value) { $fields_string .= $key . '=' . $value . '&'; } rtrim($fields_string, '&'); // Open connection $ch = curl_init(); // Set the url, number of POST vars, POST data curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, count($fields)); curl_setopt($ch, CURLOPT_POSTFIELDS, $fields_string); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_ENCODING, 'UTF-8'); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); curl_setopt($ch, CURLOPT_USERAGENT, 'AndroidTranslate/5.3.0.RC02.130475354-53000263 5.1 phone TRANSLATE_OPM5_TEST_1'); // Execute post $result = curl_exec($ch); // Close connection curl_close($ch); return $result; } /** * Dump of the JSON's response in an array * * @param string $json * The JSON object returned by the request function * * @return string A single string with the translation */ protected static function getSentencesFromJSON($json) { $sentencesArray = json_decode($json, true); $sentences = ""; foreach ($sentencesArray["sentences"] as $s) { $sentences .= isset($s["trans"]) ? $s["trans"] : ''; } return $sentences; } } echo (new GoogleTranslate())->translate("en","vi","hello");
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/N57K6
function name:  (null)
number of ops:  9
compiled vars:  none
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
  110     0  E >   NEW                                              $0      'GoogleTranslate'
          1        DO_FCALL                                      0          
          2        INIT_METHOD_CALL                                         $0, 'translate'
          3        SEND_VAL_EX                                              'en'
          4        SEND_VAL_EX                                              'vi'
          5        SEND_VAL_EX                                              'hello'
          6        DO_FCALL                                      0  $2      
          7        ECHO                                                     $2
          8      > RETURN                                                   1

Class GoogleTranslate:
Function translate:
Finding entry points
Branch analysis from position: 0
1 jumps found. (Code = 62) Position 1 = -2
filename:       /in/N57K6
function name:  translate
number of ops:  15
compiled vars:  !0 = $source, !1 = $target, !2 = $text, !3 = $response, !4 = $translation
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   17     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
   20     3        INIT_STATIC_METHOD_CALL                                  'requestTranslation'
          4        SEND_VAR_EX                                              !0
          5        SEND_VAR_EX                                              !1
          6        SEND_VAR_EX                                              !2
          7        DO_FCALL                                      0  $5      
          8        ASSIGN                                                   !3, $5
   26     9        INIT_STATIC_METHOD_CALL                                  'getSentencesFromJSON'
         10        SEND_VAR_EX                                              !3
         11        DO_FCALL                                      0  $7      
         12        ASSIGN                                                   !4, $7
   28    13      > RETURN                                                   !4
   29    14*     > RETURN                                                   null

End of function translate

Function requesttranslation:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 43) Position 1 = 21, Position 2 = 25
Branch analysis from position: 21
1 jumps found. (Code = 108) Position 1 = -2
Branch analysis from position: 25
2 jumps found. (Code = 77) Position 1 = 27, Position 2 = 34
Branch analysis from position: 27
2 jumps found. (Code = 78) Position 1 = 28, Position 2 = 34
Branch analysis from position: 28
1 jumps found. (Code = 42) Position 1 = 27
Branch analysis from position: 27
Branch analysis from position: 34
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 34
filename:       /in/N57K6
function name:  requestTranslation
number of ops:  100
compiled vars:  !0 = $source, !1 = $target, !2 = $text, !3 = $url, !4 = $fields, !5 = $fields_string, !6 = $value, !7 = $key, !8 = $ch, !9 = $result
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   45     0  E >   RECV                                             !0      
          1        RECV                                             !1      
          2        RECV                                             !2      
   49     3        ASSIGN                                                   !3, 'https%3A%2F%2Ftranslate.google.com%2Ftranslate_a%2Fsingle%3Fclient%3Dat%26dt%3Dt%26dt%3Dld%26dt%3Dqca%26dt%3Drm%26dt%3Dbd%26dj%3D1%26hl%3Des-ES%26ie%3DUTF-8%26oe%3DUTF-8%26inputm%3D2%26otf%3D2%26iid%3D1dd3b944-fa62-4b55-b330-74909a99969e'
   52     4        INIT_FCALL                                               'urlencode'
          5        SEND_VAR                                                 !0
          6        DO_ICALL                                         $11     
          7        INIT_ARRAY                                       ~12     $11, 'sl'
   53     8        INIT_FCALL                                               'urlencode'
          9        SEND_VAR                                                 !1
         10        DO_ICALL                                         $13     
         11        ADD_ARRAY_ELEMENT                                ~12     $13, 'tl'
   54    12        INIT_FCALL                                               'urlencode'
         13        SEND_VAR                                                 !2
         14        DO_ICALL                                         $14     
         15        ADD_ARRAY_ELEMENT                                ~12     $14, 'q'
   51    16        ASSIGN                                                   !4, ~12
   57    17        FETCH_DIM_R                                      ~16     !4, 'q'
         18        STRLEN                                           ~17     ~16
         19        IS_SMALLER_OR_EQUAL                                      5000, ~17
         20      > JMPZ                                                     ~18, ->25
   58    21    >   NEW                                              $19     'Exception'
         22        SEND_VAL_EX                                              'Maximum+number+of+characters+exceeded%3A+5000'
         23        DO_FCALL                                      0          
         24      > THROW                                         0          $19
   61    25    >   ASSIGN                                                   !5, ''
   62    26      > FE_RESET_R                                       $22     !4, ->34
         27    > > FE_FETCH_R                                       ~23     $22, !6, ->34
         28    >   ASSIGN                                                   !7, ~23
   63    29        CONCAT                                           ~25     !7, '%3D'
         30        CONCAT                                           ~26     ~25, !6
         31        CONCAT                                           ~27     ~26, '%26'
         32        ASSIGN_OP                                     8          !5, ~27
   62    33      > JMP                                                      ->27
         34    >   FE_FREE                                                  $22
   66    35        INIT_FCALL                                               'rtrim'
         36        SEND_VAR                                                 !5
         37        SEND_VAL                                                 '%26'
         38        DO_ICALL                                                 
   69    39        INIT_FCALL_BY_NAME                                       'curl_init'
         40        DO_FCALL                                      0  $30     
         41        ASSIGN                                                   !8, $30
   72    42        INIT_FCALL_BY_NAME                                       'curl_setopt'
         43        SEND_VAR_EX                                              !8
         44        FETCH_CONSTANT                                   ~32     'CURLOPT_URL'
         45        SEND_VAL_EX                                              ~32
         46        SEND_VAR_EX                                              !3
         47        DO_FCALL                                      0          
   73    48        INIT_FCALL_BY_NAME                                       'curl_setopt'
         49        SEND_VAR_EX                                              !8
         50        FETCH_CONSTANT                                   ~34     'CURLOPT_POST'
         51        SEND_VAL_EX                                              ~34
         52        COUNT                                            ~35     !4
         53        SEND_VAL_EX                                              ~35
         54        DO_FCALL                                      0          
   74    55        INIT_FCALL_BY_NAME                                       'curl_setopt'
         56        SEND_VAR_EX                                              !8
         57        FETCH_CONSTANT                                   ~37     'CURLOPT_POSTFIELDS'
         58        SEND_VAL_EX                                              ~37
         59        SEND_VAR_EX                                              !5
         60        DO_FCALL                                      0          
   75    61        INIT_FCALL_BY_NAME                                       'curl_setopt'
         62        SEND_VAR_EX                                              !8
         63        FETCH_CONSTANT                                   ~39     'CURLOPT_RETURNTRANSFER'
         64        SEND_VAL_EX                                              ~39
         65        SEND_VAL_EX                                              <true>
         66        DO_FCALL                                      0          
   76    67        INIT_FCALL_BY_NAME                                       'curl_setopt'
         68        SEND_VAR_EX                                              !8
         69        FETCH_CONSTANT                                   ~41     'CURLOPT_ENCODING'
         70        SEND_VAL_EX                                              ~41
         71        SEND_VAL_EX                                              'UTF-8'
         72        DO_FCALL                                      0          
   77    73        INIT_FCALL_BY_NAME                                       'curl_setopt'
         74        SEND_VAR_EX                                              !8
         75        FETCH_CONSTANT                                   ~43     'CURLOPT_SSL_VERIFYPEER'
         76        SEND_VAL_EX                                              ~43
         77        SEND_VAL_EX                                              <false>
         78        DO_FCALL                                      0          
   78    79        INIT_FCALL_BY_NAME                                       'curl_setopt'
         80        SEND_VAR_EX                                              !8
         81        FETCH_CONSTANT                                   ~45     'CURLOPT_SSL_VERIFYHOST'
         82        SEND_VAL_EX                                              ~45
         83        SEND_VAL_EX                                              <false>
         84        DO_FCALL                                      0          
   79    85        INIT_FCALL_BY_NAME                                       'curl_setopt'
         86        SEND_VAR_EX                                              !8
         87        FETCH_CONSTANT                                   ~47     'CURLOPT_USERAGENT'
         88        SEND_VAL_EX                                              ~47
         89        SEND_VAL_EX                                              'AndroidTranslate%2F5.3.0.RC02.130475354-53000263+5.1+phone+TRANSLATE_OPM5_TEST_1'
         90        DO_FCALL                                      0          
   82    91        INIT_FCALL_BY_NAME                                       'curl_exec'
         92        SEND_VAR_EX                                              !8
         93        DO_FCALL                                      0  $49     
         94        ASSIGN                                                   !9, $49
   85    95        INIT_FCALL_BY_NAME                                       'curl_close'
         96        SEND_VAR_EX                                              !8
         97        DO_FCALL                                      0          
   87    98      > RETURN                                                   !9
   88    99*     > RETURN                                                   null

End of function requesttranslation

Function getsentencesfromjson:
Finding entry points
Branch analysis from position: 0
2 jumps found. (Code = 77) Position 1 = 9, Position 2 = 18
Branch analysis from position: 9
2 jumps found. (Code = 78) Position 1 = 10, Position 2 = 18
Branch analysis from position: 10
2 jumps found. (Code = 43) Position 1 = 12, Position 2 = 15
Branch analysis from position: 12
1 jumps found. (Code = 42) Position 1 = 16
Branch analysis from position: 16
1 jumps found. (Code = 42) Position 1 = 9
Branch analysis from position: 9
Branch analysis from position: 15
1 jumps found. (Code = 42) Position 1 = 9
Branch analysis from position: 9
Branch analysis from position: 18
1 jumps found. (Code = 62) Position 1 = -2
Branch analysis from position: 18
filename:       /in/N57K6
function name:  getSentencesFromJSON
number of ops:  21
compiled vars:  !0 = $json, !1 = $sentencesArray, !2 = $sentences, !3 = $s
line      #* E I O op                           fetch          ext  return  operands
-------------------------------------------------------------------------------------
   98     0  E >   RECV                                             !0      
  100     1        INIT_FCALL                                               'json_decode'
          2        SEND_VAR                                                 !0
          3        SEND_VAL                                                 <true>
          4        DO_ICALL                                         $4      
          5        ASSIGN                                                   !1, $4
  101     6        ASSIGN                                                   !2, ''
  103     7        FETCH_DIM_R                                      ~7      !1, 'sentences'
          8      > FE_RESET_R                                       $8      ~7, ->18
          9    > > FE_FETCH_R                                               $8, !3, ->18
  104    10    >   ISSET_ISEMPTY_DIM_OBJ                         0          !3, 'trans'
         11      > JMPZ                                                     ~9, ->15
         12    >   FETCH_DIM_R                                      ~10     !3, 'trans'
         13        QM_ASSIGN                                        ~11     ~10
         14      > JMP                                                      ->16
         15    >   QM_ASSIGN                                        ~11     ''
         16    >   ASSIGN_OP                                     8          !2, ~11
  103    17      > JMP                                                      ->9
         18    >   FE_FREE                                                  $8
  107    19      > RETURN                                                   !2
  108    20*     > RETURN                                                   null

End of function getsentencesfromjson

End of class GoogleTranslate.

Generated using Vulcan Logic Dumper, using php 8.0.0


preferences:
433.17 ms | 1025 KiB | 16 Q