2012-02-26 33 views
0

我想要獲得一個聲音文件打印在一個PHP頁面上。我使用下面的代碼。我想當我運行這個腳本時,它會顯示一個可下載的聲音文件,但它會打印一些文本。你認爲我應該怎麼做才能解決這個問題?file_get_contents打印文本不是聲音

<?php 
$mylanguage=$_GET["mylanguage"]; 
$soundtext=$_GET["soundtext"]; 

echo stream_get_contents("http://api.microsofttranslator.com/V2/http.svc/Speak?appId=9CF5D9435A249BB484EC6DB50FFFB94C6733DEFB&language=$mylanguage&format=audio/wav&text=$soundtext"); 
?> 
+0

'stream_get_contents'不工作的方式。看到我的回答 – 2012-02-26 14:58:33

回答

3

發送正確的標題。默認情況下,PHP發送Content-Type: text/html。如果您發送其他內容,則必須自行發送標題。

這是什麼樣的聲音文件?

+0

這是一個wav文件。我應該如何添加音頻標題? – user1215741 2012-02-26 14:48:50

+0

標題( 「內容類型:音頻/ vnd.wave」) – ckruse 2012-02-27 05:30:22

+0

我用頭( 'HTTP:// ...')得到它的工作。 – user1215741 2012-02-28 19:58:10

1

從PHP.net:

stream_get_contents

(PHP 5)

stream_get_contents - 讀取流的剩餘部分轉換爲字符串

所以它只是返回一個字符串表示的文件內容。不是文件本身。

+0

所以我能做些什麼來接收文件本身? – user1215741 2012-02-26 14:48:22

+0

使用['file_get_contents'](http://php.net/file_get_contents)。 – hakre 2012-02-26 15:51:00

1

stream_get_contents不能這樣工作。它需要現有的開放處理。
你需要的是這個,

$url = "http://api.microsofttranslator.com/V2/http.svc/Speak?appId=9CF5D9435A249BB484EC6DB50FFFB94C6733DEFB&language=$mylanguage&format=audio/wav&text=$soundtext"; 
$fh = fopen($url, "rb"); // this rb makes it binary safe 
$audio_data = stream_get_contents($fh) 

要將其回顯到客戶端,你需要適當Content-Type頭。請檢查/etc/mime.types以獲取適當的內容類型。

假設它是一個波形音頻。使用下面的代碼。

header("Content-type: audio/x-wav"); 
echo $audio_data; 
+1

相反stream_get_contents的,它可能是更好的調用stream_copy_to_stream($ FH,的fopen( 'PHP://輸出', 'W')); – Evert 2012-02-26 15:39:58

+0

@Evert什麼關於頭?瀏覽器會識別它嗎? – 2012-02-26 16:46:30

+0

是啊,還可以做頭()第一 – Evert 2012-02-26 19:51:03

0

這是在MS API網站的例子:http://msdn.microsoft.com/en-us/library/ff512420.aspx#phpexample

在過去try塊就可以看到頭設置header('Content-Type: audio/mp3');

<?php 

class AccessTokenAuthentication { 
    /* 
    * Get the access token. 
    * 
    * @param string $grantType Grant type. 
    * @param string $scopeUrl  Application Scope URL. 
    * @param string $clientID  Application client ID. 
    * @param string $clientSecret Application client ID. 
    * @param string $authUrl  Oauth Url. 
    * 
    * @return string. 
    */ 
    function getTokens($grantType, $scopeUrl, $clientID, $clientSecret, $authUrl){ 
     try { 
      //Initialize the Curl Session. 
      $ch = curl_init(); 
      //Create the request Array. 
      $paramArr = array (
       'grant_type' => $grantType, 
       'scope'   => $scopeUrl, 
       'client_id'  => $clientID, 
       'client_secret' => $clientSecret 
      ); 
      //Create an Http Query.// 
      $paramArr = http_build_query($paramArr); 
      //Set the Curl URL. 
      curl_setopt($ch, CURLOPT_URL, $authUrl); 
      //Set HTTP POST Request. 
      curl_setopt($ch, CURLOPT_POST, TRUE); 
      //Set data to POST in HTTP "POST" Operation. 
      curl_setopt($ch, CURLOPT_POSTFIELDS, $paramArr); 
      //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); 
      //Execute the cURL session. 
      $strResponse = curl_exec($ch); 
      //Get the Error Code returned by Curl. 
      $curlErrno = curl_errno($ch); 
      if($curlErrno){ 
       $curlError = curl_error($ch); 
       throw new Exception($curlError); 
      } 
      //Close the Curl Session. 
      curl_close($ch); 
      //Decode the returned JSON string. 
      $objResponse = json_decode($strResponse); 
      if ($objResponse->error){ 
       throw new Exception($objResponse->error_description); 
      } 
      return $objResponse->access_token; 
     } catch (Exception $e) { 
      echo "Exception-".$e->getMessage(); 
     } 
    } 
} 

Class HTTPTranslator { 
    /* 
    * Create and execute the HTTP CURL request. 
    * 
    * @param string $url  HTTP Url. 
    * @param string $authHeader Authorization Header string. 
    * 
    * @return string. 
    * 
    */ 
    function curlRequest($url, $authHeader){ 
     //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)); 
     //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); 
     //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($curlError); 
     } 
     //Close a cURL session. 
     curl_close($ch); 
     return $curlResponse; 
    } 
} 
try { 
    //Client ID of the application. 
    $clientID  = "clientid"; 
    //Client Secret key of the application. 
    $clientSecret = "clientsecret"; 
    //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; 

    //Set the params. 
    $inputStr = "Welcome"; 
    $language = 'en'; 
    $params = "text=$inputStr&language=$language&format=audio/mp3"; 

    //HTTP Speak method URL. 
    $url = "http://api.microsofttranslator.com/V2/Http.svc/Speak?$params"; 
    //Set the Header Content Type. 
    header('Content-Type: audio/mp3'); 

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

    //Call the curlRequest. 
    $strResponse = $translatorObj->curlRequest($url, $authHeader); 
    echo $strResponse; 

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