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 javaFX利用scene builder的fxml文件进行开发时的label setText方法未生效问题
  • ¥15 notepad++ 自动补全提示,如输入汉字"人名",就会出现"张三","李四"
  • ¥15 如何用matlab实现PSO算法与两种智能优化算法在30维和50维问题上的性能比较?
  • ¥15 在qt的QGraphicsView和QGraphicsScene中遇到的问题
  • ¥50 如何完美解决谷歌礼品卡支付不被检测
  • ¥15 nslt的可用模型,或者其他可以进行推理的现有模型
  • ¥15 arduino上连sim900a实现连接mqtt服务器
  • ¥15 vncviewer7.0安装后如何正确注册License许可证,激活使用
  • ¥15 phython如何实现以下功能?查找同一用户名的消费金额合并2
  • ¥66 关于人体营养与饮食规划的线性规划模型