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.