我正在尝试在php页面上打印声音文件。我正在使用下面的代码。我想当我运行这个脚本时,它会显示一个要下载的声音文件,但它会打印一些文本。您认为我应该怎么做才能解决这个问题?
<?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 :(得分:3)
发送正确的标题。默认情况下,PHP发送Content-Type: text/html
。如果您发送其他内容,则必须自行发送标题。
这是什么样的声音文件?
答案 1 :(得分:1)
来自PHP.net:
stream_get_contents
(PHP 5)
stream_get_contents - 将流的剩余部分读入字符串
因此它只返回文件内容的字符串表示。不是文件本身。
答案 2 :(得分: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;
答案 3 :(得分: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;
}
?>