【发布时间】:2016-01-29 18:46:34
【问题描述】:
我正在编写一个 jQuery 翻译插件。我的第一次迭代传递了一个要翻译的字符串数组。尽管这工作正常,但速度非常慢。我修改了插件,一次只传递一个单词。两个例程都使用 ajax 调用如下所示的 PHP Translate 函数。我已检查以确保 ajax 调用正常工作并正确传递单词。我还检查了与警告有关的答案,但无济于事。
翻译第一个单词后,我会收到以下针对每个单词重复的消息。在多次警告后,我还收到一些 curl 错误,抱怨它无法连接到主机(Translate-Exception: curl=couldn't connect to host)。
警告
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 .php 在第 170 行
警告:simplexml_load_string() [function.simplexml-load-string]: ^ in D:\hshome\c287577\test.bunkerhill.com\php\translate.php 第 170 行
警告:simplexml_load_string() [function.simplexml-load-string]:实体:第 3 行:解析器错误:开始和结束标记不匹配:META 第 3 行和 HEAD 在 D:\hshome\c287577\test.bunkerhill.com \php\translate.php 在第 170 行
警告:simplexml_load_string() [function.simplexml-load-string]:在 D:\hshome\c287577\test.bunkerhill.com\php\translate.php 第 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;
}
}
卷曲代码
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;
}
似乎存在某种时间问题,但我无法确定。希望有人遇到过类似的问题,并能对这种痛苦的情况有所了解。
【问题讨论】:
-
谢谢杰。你是怎么做到的?
-
Jay 是魔术师 ;) 实际上编辑器中有一个块引用按钮。
-
使用@simplexml_load_string($curlResponse) 代替:p
-
谢谢。这消除了警告,但大多数翻译的单词都是空白的。我仍然收到“无法连接到主机”异常。