2012-02-26 62 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; 
} 
?>