0

我有问题。我正在使用 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;
}
?>
4

1 回答 1

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;
?>
于 2014-07-17T16:37:03.763 回答