0

문제가 있습니다. 나는 자바 스크립트와 PHP로 일하고있다. 이 PHP 코드는 다양한 echo :: D에서 볼 수 있듯이 변수 $ curlResponse까지 실행됩니다. 이 변수와 다른 모든 변수 ($ xmlObj, $ translatedStr, $ translatedText)는 비어 있습니다! 누구든지 나를 도울 수 있습니까?빙 번역기 API : 변수 텍스트가 비어 있음

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

답변

0

당신이 클래스 파일을 가지고 곳에서 지정하고 왜이 소스 코드를 사용하지 마십시오 내가이 사이트에서 클래스 파일을 가지고 잘

<?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; 
?> 
+0

작업 믿고 사용할 수 Here msdn.microsoft.com/en-us/library/ff512421.aspx ... – user3849955