2012-09-24 145 views
0

我在我的代碼中使用了捲曲函數。我的CURLINFO_HTTP_CODE總是返回0,當我使用 curl_error($ ch)時,它返回'無法到達主機'。我的主機是ispeech,它不應該有問題。有人可以幫我嗎?非常感謝!php捲曲函數問題

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(); 
+0

在功能makeRequest的,語句'$ URL = $後this->服務器。 「/?」 。 http_build_query($ this-> parameters);',var_dump($ url);'show?還要確保你的服務器正確解析了'api.ispeech.org',並且你沒有防火牆阻止curl請求。 – drew010

+0

'echo'和'curl_exec()'之間需要一個空格 – Robbie

回答

1

所以保存與OB緩衝各地faffing,你可以取代

ob_start(); 

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

$http_body = curl_exec($ch); 

還增加了回聲和curl_exec缺失之間的空間,從你的例子(儘管這應該拋出一個致命和停止執行? - 你有你自己的錯誤處理程序)