php http_build_query错误“0 =”

时间:2013-07-26 19:23:20

标签: php

我希望使用此代码发布网址并获取结果,但在每次结果之前添加= 0

我的代码是

    <!DOCTYPE HTML>
<html>
<body>
<h1>In this demonstration:<br />
>tts is done on server side (i.e by using Google-translate server)<br/>
>then the audio received from Google-translate server is saved on your server (local/production)<br />
>and then that saved audio is played through that saved file on this webpage.</h1>
<h3>
Tested with:
Chrome v21 [Working],
Firefox v14 [Not Working, firefox does not support mp3 audio format playback],
IE v9[Working]
</h3>
<hr />
<form method="POST">
Text to convert : <input name="txt" type="text" /><br />
Filename to save (without the extension) : <input name="filename" type="text" /><br />
Convert text to speech : <input name="submit" type="submit" value="Convert" />
</form>

<?php
if (isset($_POST['txt']) && isset($_POST['filename']))
{
    $text=htmlentities($_POST['txt']);
    $filename=$_POST['filename'].'.mp3';

    $querystring = http_build_query(array($text));

    if ($soundfile = file_get_contents("http://api.voicerss.org?key=c68635f1104b452e8dbe740c0c0330f3&src=$querystring&hl=en-in j"))
    {
        file_put_contents($filename,$soundfile);
        echo ('
            <audio autoplay="autoplay" controls="controls">
            <source src="'.$filename.'" type="audio/mp3" />
            </audio>
            <br />
            Saved mp3 location : '.dirname(__FILE__).'\\'.$filename.'
            <br />
            Saved mp3 uri : <a href="'.$filename.'">'.$_SERVER['SERVER_NAME'].'/webtts/'.$filename.'</a>'
        );
    }
    else echo("<br />Audio could not be saved");
}
?>

结果是 http://api.voicerss.org?key=c68635f1104b452e8dbe740c0c0330f3&src=0=Good+evening.+Please+sit+down.+Now+tell+me+about+your+problem&hl=en-in

它不应该显示src = 0 =好,它应该显示src = Good +晚上,如何删除0 =

1 个答案:

答案 0 :(得分:6)

您需要为http_build_query

提供关联数组

所以改变

 http_build_query(array($text))

 http_build_query(array("src"=>$text))

 file_get_contents("http://api.voicerss.org?key=c68635f1104b452e8dbe740c0c0330f3&src=$querystring&hl=en-in j

file_get_contents("http://api.voicerss.org?key=c68635f1104b452e8dbe740c0c0330f3&$querystring&hl=en-in j

或者你可以使用

http_build_query(array(
     "src"=>$text,
     "key"=>"c68635f1104b452e8dbe740c0c0330f3",
     "hl"=>"en-in j"))

file_get_contents("http://api.voicerss.org?".$querystring);