简体   繁体   中英

file_get_contents (and wget) very slow

I'm using the google text to speech api, but for some reason it's being really slow when I connect to it via php or command line.

I'm doing this:

$this->mp3data = file_get_contents("http://translate.google.com/translate_tts?tl=en&q={$text}");  

Where $text is just a urlencoded string.

I've also tried doing it via wget on the command line:

wget http://translate.google.com/translate_tts?tl=en&q=test

Either way takes about 20 seconds or more. Via php it does eventually get the contents and add them to a new file on my server as I want it to. Via wget it times the connection out.

However, if I just go to that url in the browser, it's pretty much instant.

Could anyone shed any light on why this might be occuring?

Thanks.

It's due to how Google parses robots. You need to spoof the User-Agent headers to pretend to be a computer.

Some info on how to go about this would be here:

https://duckduckgo.com/?q=php%20curl%20spoof%20user%20agent

Managed to sort this out now, this is what I ended up doing and now it's only taking a few seconds:

$header=array("Content-Type: audio/mpeg"); 
        $ch = curl_init(); 
        curl_setopt($ch, CURLOPT_URL, $uri); 
        curl_setopt($ch, CURLOPT_HEADER, false); 
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header); 
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 30); 
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
        curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.13) Gecko/20080311 Firefox/2.0.0.13');
        $this->mp3data = curl_exec($ch); 
        curl_close($ch); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM