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

为什么我会收到这些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条回答 默认 最新

      报告相同问题?

      相关推荐 更多相似问题

      悬赏问题

      • ¥15 vsc C++ 终端开头有大量空行
      • ¥20 【matlab】运输问题优化问题。
      • ¥15 nodejs express 和 axios 前后端对接,数据体类型
      • ¥15 fluent计算后处理中如何把质量分数和摩尔分数转化为体积分数
      • ¥20 asn1c编码问题,懂asn1的人很简单
      • ¥15 使用vs2019开发的动态链路库在win7环境下使用有问题
      • ¥30 quartus prime画电路图,要发源文件和电路图每个模块批注,源程序和仿真结果
      • ¥15 MFC滚动条如何正确使用
      • ¥15 缓冲区算法求面积,基于文献
      • ¥50 电路PCB原理图设计AD19