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

为什么我会收到这些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.

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

报告相同问题?

悬赏问题

  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化
  • ¥15 Mirare PLUS 进行密钥认证?(详解)
  • ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
  • ¥20 想用ollama做一个自己的AI数据库