我的代码中的 curl 函数有问题。我的 CURLINFO_HTTP_CODE 总是返回 0,当我使用 curl_error($ch) 它返回“无法到达主机”。我的主机是ispeech,它应该没有问题。这里有人可以帮我吗?非常感谢!
iSpeech.php
class iSpeechBase{
var $server;
var $parameters = array("device-type"=>"php-SDK-0.3");
function setParameter($parameter, $value){
if ($parameter == "server")
$this->server = $value;
else
$this->parameters["$parameter"] = $value;
}
function makeRequest(){
$ch = curl_init();
$url=$this->server . "/?" . http_build_query($this->parameters);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
ob_start();
echocurl_exec($ch);
$http_body = ob_get_contents();
ob_end_clean();
echo curl_getinfo($ch, CURLINFO_HTTP_CODE); //return 0
echo curl_error($ch); //return Could not reach host.
if (curl_getinfo($ch, CURLINFO_HTTP_CODE) != 200)
if ($this->parameters["action"] == "convert")
return array("error" => $http_body);
return $http_body;
}
}
综合-demo.php
require_once('ispeech.php');
$SpeechSynthesizer = new SpeechSynthesizer();
$SpeechSynthesizer->setParameter("server", "http://api.ispeech.org/api/rest");
$SpeechSynthesizer->setParameter("apikey", "myapikey");
$SpeechSynthesizer->setParameter("text", "yes");
$SpeechSynthesizer->setParameter("format", "wav");
$SpeechSynthesizer->setParameter("voice", "usenglishfemale");
$SpeechSynthesizer->setParameter("output", "rest");
$result = $SpeechSynthesizer->makeRequest();