2016-01-29 77 views
1

我在寫一個jQuery翻譯插件。我的第一次迭代傳遞了一串字符串來轉換。雖然這工作得很好,但速度非常慢。我修改了插件一次傳遞一個單詞。這兩個例程都使用ajax來調用下面顯示的PHP Translate函數。我已經檢查確保ajax調用正在工作並正確傳遞這些單詞。我也檢查了有關警告的答案,但無濟於事。爲什麼我會得到這些PHP警告:simplexml_load_string

翻譯完第一個單詞後,我得到以下消息,爲每個單詞重複。我也收到了一些curl錯誤,抱怨它無法連接到主機(Translate-Exception:curl =無法連接到主機),經過多次警告。

警告

Startseite(第一個字被正確翻譯)

警告:simplexml_load_string()[function.simplexml負荷字符串]:實體:第1行:分析器錯誤:空間在D:\ hshome \ c287577 \ test.bunkerhill.com \ php \ translate.php上的公共標識符後面需要17012行

警告:simplexml_load_string()[function.simplexml-load-string]:http:// D:\ hshome \ c287577 \ tes中的www.w3.org/TR/html4/str t.bunkerhill.com \ php \ translate.php on line 170

Warning:simplexml_load_string()[function.simplexml-load-string]:^在D:\ hshome \ c287577 \ test.bunkerhill.com \ php \ translate.php on line 170

Warning:simplexml_load_string()[function.simplexml-load-string]:Entity:line 3:parser error:打開和結束標記不匹配:META第3行和HEAD在D:\ hshome \ c287577 \ test.bunkerhill.com \ php \ translate.php on line 170

Warning:simplexml_load_string()[function.simplexml-load-string]:in D:\ hshome \ c287577 \ test.bunkerhill.com \ php \ translate.php on line 170

PHP

public function Translate() { 
    try { 
     if (!empty($_POST['text'])) 
      $text = $_POST['text']; 
      else 
      throw new RuntimeException('Programmer Error: text to translate not found or is empty!'); 
     $auth = new AccessTokenAuthentication(); 
     $authHeader=$auth->authenticate(); 
     $fromLanguage = empty($_POST['fromLang']) ? substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2) : $_POST['fromLang']; 
     $toLanguage = empty($_POST['toLang']) ? substr($_SERVER['HTTP_ACCEPT_LANGUAGE'], 0, 2) : $_POST['toLang']; 
     $contentType = 'text/plain'; 
     $category  = 'general'; 
     //Create the Translator Object. 
     $translatorObj = new HTTPTranslator(); 
     //$translated = array(); 
     //foreach ($elements as $element) { 
      $params = "text=".$text."&to=".$toLanguage."&from=".$fromLanguage; 
      $translateUrl = "http://api.microsofttranslator.com/v2/Http.svc/Translate?$params"; 
      //Get the curlResponse. 
      $curlResponse = $translatorObj->curlRequest($translateUrl, $authHeader); 
      //Interprets a string of XML into an object. 
      $xmlObj = simplexml_load_string($curlResponse); 
      echo $xmlObj[0]; 
      /* unset($translatorObj); 

      foreach((array)$xmlObj[0] as $val){ 
       array_push($translated, $val); 
      } 
      } end-foreach 
       echo json_encode($translated); 
      */ 
    } 
    catch (Exception $e) { 
      echo "Translate-Exception: " . $e->getMessage() . PHP_EOL; 
    } 
} 

捲曲CODE

function curlRequest($url, $authHeader, $postData='') 
{ 
    //Initialize the Curl Session. 
    $ch = curl_init(); 
    //Set the Curl url. 
    curl_setopt ($ch, CURLOPT_URL, $url); 
    //Set the HTTP HEADER Fields. 
    curl_setopt ($ch, CURLOPT_HTTPHEADER, array($authHeader,"Content-Type: text/xml")); 
    //CURLOPT_RETURNTRANSFER- TRUE to return the transfer as a string of the return value of curl_exec(). 
    curl_setopt ($ch, CURLOPT_RETURNTRANSFER, TRUE); 
    //CURLOPT_SSL_VERIFYPEER- Set FALSE to stop cURL from verifying the peer's certificate. 
    curl_setopt ($ch, CURLOPT_SSL_VERIFYPEER, False); 
    if($postData) { 
     //Set HTTP POST Request. 
     curl_setopt($ch, CURLOPT_POST, TRUE); 
     //Set data to POST in HTTP "POST" Operation. 
     curl_setopt($ch, CURLOPT_POSTFIELDS, $postData); 
    } 
    //Execute the cURL session. 
    $curlResponse = curl_exec($ch); 
    //Get the Error Code returned by Curl. 
    $curlErrno = curl_errno($ch); 
    if ($curlErrno) { 
     $curlError = curl_error($ch); 
     throw new Exception('curl='.$curlError); 
    } 
    //Close a cURL session. 
    curl_close($ch); 
    if (trim($curlResponse)=='') echo 'c-ressp='.$curlResponse; 
    return $curlResponse; 
} 

似乎有某種計時問題,但我不能確定它。希望在那裏有人遇到過類似的問題,並且可以對這種痛苦的情況有所瞭解。

+0

謝謝Jay。你是怎麼做到的? –

+0

傑伊是魔術師;)編輯器中實際上有一個blockquote按鈕。 –

+0

使用@simplexml_load_string($ curlResponse)代替:p – webzar

回答

0

爲了解決這個問題,我決定用JavaScript代替PHP重寫翻譯。它現在比PHP版本快得多。

相關問題