0

我的代码中的 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();
4

1 回答 1

1

因此,请不要使用 ob 缓冲区,您可以替换

ob_start(); 

echocurl_exec($ch); 
$http_body = ob_get_contents(); 
ob_end_clean(); 

$http_body = curl_exec($ch);

还添加了示例中缺少的 echo 和 curl_exec 之间的空格(尽管这应该会引发致命并停止执行 - 你有自己的错误处理程序吗?)

于 2012-09-24T23:57:12.483 回答