duan32342 2016-01-29 18:46
浏览 487
已采纳

为什么我会收到这些PHP警告:simplexml_load_string

I am writing a jQuery Translation Plugin. My first iteration passed an array of strings to translate. Although this worked fine, it was dreadfully slow. I modified the plugin to pass a single word at a time. Both routines use ajax to call the PHP Translate function shown below. I have checked to ensure the ajax calls are working and properly passing the words. I have also checked answers relating to the warnings to no avail.

After the first word is translated, I get the following messages that repeat for each word. I also get a couple of curl errors complaining it could not connect to host (Translate-Exception: curl=couldn't connect to host) after numerous warnings.

Warnings

Startseite (the first word is translated correctly)

Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 1: parser error : Space required after the Public Identifier in D:\hshome\c287577\test.bunkerhill.com\php\translate.php on line 170

Warning: simplexml_load_string() [function.simplexml-load-string]: http://www.w3.org/TR/html4/str in 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

Warning: simplexml_load_string() [function.simplexml-load-string]: Entity: line 3: parser error : Opening and ending tag mismatch: META line 3 and HEAD in 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;
    }
}

CURL 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;
}

It seems there is some kind of timing issue, but I cannot identify it. Hopefully someone out there has experienced a similar problem and can shed some light on this painful situation.

  • 写回答

1条回答 默认 最新

  • douzhan8395 2016-01-30 17:28
    关注

    To solve this problem, I decided to rewrite the translation using JavaScript instead of PHP. It is now infinitely faster than the PHP version.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 目标检测项目无法读取视频
  • ¥15 GEO datasets中基因芯片数据仅仅提供了normalized signal如何进行差异分析
  • ¥15 小红薯封设备能解决的来
  • ¥100 求采集电商背景音乐的方法
  • ¥15 数学建模竞赛求指导帮助
  • ¥15 STM32控制MAX7219问题求解答
  • ¥20 在本地部署CHATRWKV时遇到了AttributeError: 'str' object has no attribute 'requires_grad'
  • ¥15 vue+element项目中多tag时,切换Tab时iframe套第三方html页面需要实现不刷新
  • ¥50 深度强化学习解决能源调度问题
  • ¥15 一道计算机组成原理问题