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

时间:2016-01-29 18:46:34

标签: php jquery ajax curl

我正在编写一个jQuery Translation Plugin。我的第一次迭代传递了一个字符串数组来翻译。虽然这很好,但速度非常慢。我修改了插件,一次传递一个单词。这两个例程都使用ajax来调用下面显示的PHP Translate函数。我检查过以确保ajax调用正常并且正确传递了这些单词。我也检查过与警告有关的答案无济于事。

翻译完第一个单词后,我会收到以下每个单词重复的消息。我还得到一些卷曲错误,抱怨它无法连接到主机(翻译 - 例外:curl =无法连接到主机),经过多次警告。

警告

  

Startseite(第一个单词被正确翻译)

     

警告:simplexml_load_string()[function.simplexml-load-string]:实体:第1行:解析器错误:在D:\ hshome \ c287577 \ test.bunkerhill.com \ php \ translate中的公共标识符后需要空格。第170行的PHP

     

警告:simplexml_load_string()[function.simplexml-load-string]:http://www.w3.org/TR/html4/str在D:\ hshome \ c287577 \ test.bunkerhill.com \ php \ translate第170行的.php

     

警告:simplexml_load_string()[function.simplexml-load-string]:^在第170行的D:\ hshome \ c287577 \ test.bunkerhill.com \ php \ translate.php

     

警告:simplexml_load_string()[function.simplexml-load-string]:实体:第3行:解析器错误:打开和结束标记不匹配:META第3行和HEAD在D:\ hshome \ c287577 \ test.bunkerhill.com第170行\ php \ translate.php

     

警告:simplexml_load_string()[function.simplexml-load-string]:在第170行的D:\ hshome \ c287577 \ test.bunkerhill.com \ php \ translate.php

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

似乎存在某种时间问题,但我无法识别它。希望有人在那里经历过类似的问题,并且可以对这种痛苦的情况有所了解。

1 个答案:

答案 0 :(得分:0)

为了解决这个问题,我决定使用JavaScript而不是PHP重写翻译。它现在比PHP版本快得多。