php curl functions issue

145 Views Asked by At

I am having troble on curl functions in my codes. My CURLINFO_HTTP_CODE always return 0 and when I use curl_error($ch) it returns 'could not reach host'. My host is ispeech and it shouldn't have problems. Can anyone here help me out? Thanks a lot!

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;
        }
   }

synthesis-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();
1

There are 1 best solutions below

0
On BEST ANSWER

So save faffing around with the ob buffer, you could replace

ob_start(); 

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

with

$http_body = curl_exec($ch);

Also adds the space between echo and curl_exec missing from your example (although that should throw a fatal and stop execution - do you have your own error handler?)