PHP Classes

CURL support

Recommend this page to a friend!

      Google Translate API  >  All threads  >  CURL support  >  (Un) Subscribe thread alerts  
Subject:CURL support
Summary:patch to support translating longer text
Author:Sergey Bogomolov
Date:2010-04-18 09:15:18

  1. CURL support   Reply   Report abuse  
Picture of Sergey Bogomolov Sergey Bogomolov - 2010-04-18 09:15:18
If trying to translate text string that is longer 500 (approximately)
it is caused execution error, which even shuts down the browser.

The problem is, that fopen() function can handle limited length URLs as input parameter.

To fix the problem, I've implemented alternative method of interconnection using CURL (it is used only if CURL is supported on the server).

Also I've added the following string:
if($handle===false) die('Can not connect to API.');
To cause shutting down script execution instead of shutting down your browser in case of error %))

Here is improved code:


Author: Jamie Telin (, currently at employed

Scriptname: GoogleTranslateApi v1.1 (improved by Dialer)

//Create new object
$translate = new GoogleTranslateApi;

//How it works
$translate->FromLang = 'sv';
$translate->ToLang = 'en';
echo $translate->translate('Hej jag heter Jamie!');
//Would output; Hello my name is Jamie!

$translate->TranslatedText //Any translation is also saved in TranslatedText

$translate->FromLang //Set language to translate from
$translate->ToLang //Set language to translate to
$translate->Text //Text to translate if not passed with translate();

//Debug / Error reporting
$translate->DebugMsg //Gets all error messages
$translate->DebugStatus //Gets all status codes, 200 = ok, 400 = Invalid languages

//Google may update their API and change version. If so, you must update version number in this class.
$translate->Version = '1.0'; //Use object to change version
var $Version = '1.0'; //Change it in the source of class


class GoogleTranslateApi{

var $BaseUrl = '';
var $FromLang = 'sv';
var $ToLang = 'en';
var $Version = '1.0';

var $CallUrl;

var $Text = 'Hej v&#1076;rlden!';

var $TranslatedText;
var $DebugMsg;
var $DebugStatus;

function GoogleTranslateApi(){
$this->CallUrl = $this->BaseUrl . "?v=" . $this->Version . "&q=" . urlencode($this->Text) . "&langpair=" . $this->FromLang . "%7C" . $this->ToLang;

function makeCallUrl(){
$this->CallUrl = $this->BaseUrl . "?v=" . $this->Version . "&q=" . urlencode($this->Text) . "&langpair=" . $this->FromLang . "%7C" . $this->ToLang;

function translate($text = ''){
if($text != ''){
$this->Text = $text;
if($this->Text != '' && $this->CallUrl != ''){

$ch = curl_init($this->CallUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$handle = fopen($this->CallUrl, "rb");
if($handle===false) die('Can not connect to API.');
$contents = '';
while (!feof($handle)) {
$contents .= fread($handle, 8192);

$json = json_decode($contents, true);

if($json['responseStatus'] == 200){ //If request was ok
$this->TranslatedText = $json['responseData']['translatedText'];
$this->DebugMsg = $json['responseDetails'];
$this->DebugStatus = $json['responseStatus'];
return $this->TranslatedText;
} else { //Return some errors
return false;
$this->DebugMsg = $json['responseDetails'];
$this->DebugStatus = $json['responseStatus'];
} else {
return false;