2012-02-26 7 views
0

私は、phpページに印刷するサウンドファイルを取得しようとしています。私は以下のコードを使用しています。私はこのスクリプトを実行すると、ダウンロードするサウンドファイルを表示していたが、テキストを印刷すると思った。私はこれを解決するために何をすべきだと思いますか? PHP.netから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'がそのように動作しないを設定見ることができます。私の答えを参照してください –

答えて

3

正しいヘッダーを送信してください。デフォルトでは、PHPはContent-Type: text/htmlを送信します。あなたが何か他のものを送るなら、あなたは自分でヘッダを送る必要があります。

どのようなサウンドファイルですか?

+0

これはwavファイルです。オーディオヘッダーを追加するにはどうすればよいですか? – user1215741

+0

ヘッダー( "Content-Type:audio/vnd.wave") – ckruse

+0

ヘッダー( 'http:// ...')を使用して機能させました。 – user1215741

1

stream_get_contents

(PHP 5)

stream_get_contentsは - 列

にストリームの残りの部分を読み込みだから、単に文字列表現を返しますファイル内容のファイル自体ではありません。

+0

ファイル自体を受信するために何ができますか? – user1215741

+0

['file_get_contents'](http://php.net/file_get_contents)を使用してください。 – hakre

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:// output'、 'w'))を呼び出すほうが良いかもしれません。 – Evert

+0

@ヘッダーはどうなりますか?ブラウザはそれを認識しますか? –

+0

まだヘッダー()を最初に行うことができます – Evert

0

これは、MSのAPIサイト上の例である:最後のtryブロック内でhttp://msdn.microsoft.com/en-us/library/ff512420.aspx#phpexample

あなたはヘッダが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; 
} 
?> 
関連する問題