PHP http_build_query 错误 "0="

标签 php

您好,我正在使用此代码发布 url 并获取结果,但它在每个结果之前添加 =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=Good,应该显示src=Good+evening,如何去掉0=

最佳答案

您需要为 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);

关于PHP http_build_query 错误 "0=",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17889358/

相关文章:

php - 将值和数组添加到 PDO 执行方法

php - 如何将 session 添加到我的表单中?

php - 数据库设计 : Selecting and limiting Favorites

php - 将变量的数据拆分为单独的变量

javascript - 如何在mysql查询中精确找到选定对 Angular 线区域之间的数据

php - 尝试在一个查询中将数据插入 MySQL 表,但更改一个特定列

php - 我找不到的 php 代码中的随机语法错误

php - CakePHP 根据用户名在登录时重定向

php - 如何从 PHP 获取我的 URL 的 "application root"?

PHP字符串替换匹配整个单词