bing translator API:变量文本翻译为空

时间:2014-07-17 16:11:39

标签: javascript php language-translation bing-translator-api

我有问题。我正在使用javascript和PHP。这个PHP代码运行(从众多的echo:D中可以看到),直到变量$ curlResponse。从这个变量开始,所有其他变量跟随($ xmlObj,$ translatedStr,$ translatedText)都是空的!有人可以帮帮我吗?

<?php
try {
    $clientID       = "XXX";
    //Client Secret key of the application.
    $clientSecret = "XXX";
    //OAuth Url.
    $authUrl      = "https://datamarket.accesscontrol.windows.net/v2/OAuth2-13/";
    //Application Scope Url
    $scopeUrl     = "http://api.microsofttranslator.com";
    //Application grant type
    $grantType    = "client_credentials";
        // Create the AccessTokenAuthentication object.
        $authObj = new AccessTokenAuthentication();
        // Get the Access token.
        $accessToken = $authObj->getTokens($grantType, $scopeUrl, $clientID,           $clientSecret, $authUrl);
        // Create the authorization Header string.
        $authHeader = "Authorization: Bearer ". $accessToken;

        echo "<script type='text/javascript'>alert('$authHeader');</script>";

        // Set the parameters.
        // Sets source language. $fromLanguage = variable, langs[source][0] = name of textarea.
        $fromLanguage = $_COOKIE['cookie_source'];


        // Sets destination language. $toLanguage = variable, dest_lang = name of textarea.
        $toLanguage = $_COOKIE['cookie_dest'];

        // Sets text to translate. $inputStr = variable, source_text = content of thextarea.
        $inputStr = $_COOKIE['cookie_final'];
        echo "<script type='text/javascript'>alert('$inputStr');</script>";
        $contentType = 'text/plain';
        $category = 'general';

        // Variable that composes the string of parameters for the transaltion
        $paramst = "text=".urlencode($inputStr)."&to=".$toLanguage."&from=".$fromLanguage;

        echo "<script type='text/javascript'>alert('$paramst');</script>";


        // URL to translate the text
         $translateUrl = "http://api.microsofttranslator.com/v2/Http.svc/Translate?$paramst";
        echo "<script type='text/javascript'>alert('$translateUrl');</script>";

        //Create the Translator Object.
        $translatorObj = new HTTPTranslator();


        //Get the curlResponse.
        $curlResponse = $translatorObj->curlRequest($translateUrl, $authHeader);
        echo "<script type='text/javascript'>alert('$curlResponse');</script>";

        //Interprets a string of XML into an object.
        $xmlObj = simplexml_load_string($curlResponse);

        foreach((array)$xmlObj[0] as $val) {
            $translatedStr = $val;
        }



        echo "<script type='text/javascript'>alert('$translatedStr');</script>";

        $translatedText = urlencode($translatedStr);

        echo "<script type='text/javascript'>alert('$translatedText');</script>";
        if (isset($inputStr)== true){
            if ($translatedStr==''){

            } else {
                echo "<script type='text/javascript'>alert('e piena');</script>";
            }

        }
} catch (Exception $e) {
    echo "Exception: ".$e->getMessage().PHP_EOL;
}
?>

1 个答案:

答案 0 :(得分:0)

请指明您从哪里获得类文件,以及为什么不使用我认为可以正常使用的Here源代码

<?php
/**
 * This file will retuen JSON response
 */
require_once('config.inc.php');
require_once('class/ServicesJSON.class.php');
require_once('class/MicrosoftTranslator.class.php');

$translator = new MicrosoftTranslator(ACCOUNT_KEY);
$text_to_translate = $_REQUEST['text'];
$to = $_REQUEST['to'];
$from = $_REQUEST['from'];
$translator->translate($from, $to, $text_to_translate);
echo $translator->response->jsonResponse;
?>