Click to See Complete Forum and Search --> : [RESOLVED] Forms and Headers?


web wheeler
12-18-2007, 03:40 PM
I would like to use Google Translation to translate text from one language to another, and then to capture the translated text and store it in a MySQL database.

I have two code samples that submit my text to Google. The first one (GOOGLE TEST) submits my text using a standard HTML form with a POST. The second one (MY TEST) submits my text by crafting a POST header and variables using fsockopen and fputs.

Here are the two code samples:

GOOGLE TEST

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>
<head>
<title>Google Test</title>
<meta name='description' content='googleTest.php' />
<meta http-equiv='content-type' content='text/html; charset=UTF-8' />
</head>

<body onload='javascript:document.googleTest.submit()'>
<form name='googleTest' action='http://translate.google.com/translate_t' method='post'>
<input type='hidden' name='hl' value='en' />
<input type='hidden' name='ie' value='UTF8' />
<input type='hidden' name='text' value='-->this is a test<--' />
<input type='hidden' name='langpair' value='en|ar' />
</form>
</body>
</html>



MY TEST

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>
<head>
<title>My Test</title>
<meta name='description' content='myTest.php' />
<meta http-equiv='content-type' content='text/html; charset=UTF-8' />
</head>
<body>
<?php
$http_response = '';
$fp = fsockopen('translate.google.com', 80);
fputs($fp, "POST http://translate.google.com/translate_t HTTP/1.1\r\n");
fputs($fp, "Content-Length: 56\r\n");
fputs($fp, "Content-Type: application/x-www-form-urlencoded\r\n\r\n");
fputs($fp, "hl=en&ie=UTF8&text=-->this+is+a+test<--&langpair=en%7Car");
while (!feof($fp))
{
$http_response .= fgets($fp, 128);
}
fclose($fp);
echo $http_response;
?>
</body>
</html>

Both forms get submitted to Google OK, but then GOOGLE TEST calls http://translate.google.com/translate_t whereas MY TEST calls http://atv.bounceme.net/translate_t.js

Here are the headers:

GOOGLE TEST

http://atv.bounceme.net/atvCommon/googleTest.php
GET /atvCommon/googleTest.php HTTP/1.1
Host: atv.bounceme.net
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.8) Gecko/20071019 Fedora/2.0.0.8-1.fc7 Firefox/2.0.0.8
Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Cookie: __utma=230177612.1085281755.1198005793.1198005793.1198005793.1; __utmb=230177612; __utmc=230177612; __utmz=230177612.1198005793.1.1.utmccn=(direct)|utmcsr=(direct)|utmcmd=(none)
HTTP/1.x 200 OK
Date: Tue, 18 Dec 2007 20:26:59 GMT
Server: Apache/2.2.6 (Fedora)
X-Powered-By: PHP/5.2.4
Content-Length: 756
Connection: close
Content-Type: text/html; charset=UTF-8
----------------------------------------------------------
http://translate.google.com/translate_t
POST /translate_t HTTP/1.1
Host: translate.google.com
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.8) Gecko/20071019 Fedora/2.0.0.8-1.fc7 Firefox/2.0.0.8
Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Referer: http://atv.bounceme.net/atvCommon/googleTest.php
Cookie: __utma=195145449.337942985.1198002432.1198002432.1198002432.1; __utmb=195145449; __utmc=195145449; __utmz=195145449.1198002432.1.1.utmccn=(referral)|utmcsr=atv.bounceme.net|utmcct=/atvCommon/googleTest.php|utmcmd=referral; PREF=ID=ba0179f670a5153a:TM=1198003419:LM=1198003419:S=-n1elqQh0mDJGpMT
Content-Type: application/x-www-form-urlencoded
Content-Length: 60
hl=en&ie=UTF8&text=--%3Ethis+is+a+test%3C--&langpair=en%7Car
HTTP/1.x 200 OK
Content-Type: text/html; charset=UTF-8
Content-Language: en
Content-Encoding: gzip
Server: TWS/0.9
Cache-Control: private, x-gzip-ok=""
Content-Length: 2534
Date: Tue, 18 Dec 2007 20:43:28 GMT
----------------------------------------------------------


MY TEST

http://atv.bounceme.net/atvCommon/myTest.php
GET /atvCommon/myTest.php HTTP/1.1
Host: atv.bounceme.net
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.8) Gecko/20071019 Fedora/2.0.0.8-1.fc7 Firefox/2.0.0.8
Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Cookie: __utma=230177612.1085281755.1198005793.1198005793.1198005793.1; __utmb=230177612; __utmc=230177612; __utmz=230177612.1198005793.1.1.utmccn=(direct)|utmcsr=(direct)|utmcmd=(none)
HTTP/1.x 200 OK
Date: Tue, 18 Dec 2007 20:25:51 GMT
Server: Apache/2.2.6 (Fedora)
X-Powered-By: PHP/5.2.4
Connection: close
Transfer-Encoding: chunked
Content-Type: text/html; charset=UTF-8
----------------------------------------------------------
http://atv.bounceme.net/translate_t.js
GET /translate_t.js HTTP/1.1
Host: atv.bounceme.net
User-Agent: Mozilla/5.0 (X11; U; Linux i686; en-US; rv:1.8.1.8) Gecko/20071019 Fedora/2.0.0.8-1.fc7 Firefox/2.0.0.8
Accept: */*
Accept-Language: en-us,en;q=0.5
Accept-Encoding: gzip,deflate
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7
Keep-Alive: 300
Connection: keep-alive
Referer: http://atv.bounceme.net/atvCommon/myTest.php
Cookie: __utma=230177612.1085281755.1198005793.1198005793.1198005793.1; __utmb=230177612; __utmc=230177612; __utmz=230177612.1198005793.1.1.utmccn=(direct)|utmcsr=(direct)|utmcmd=(none)
HTTP/1.x 404 Not Found
Date: Tue, 18 Dec 2007 20:26:06 GMT
Server: Apache/2.2.6 (Fedora)
Content-Length: 293
Connection: close
Content-Type: text/html; charset=iso-8859-1
----------------------------------------------------------


Why is there a difference between the two form submissions, and what can I do to get MY TEST to do the same thing that GOOGLE TEST does?

TJ111
12-18-2007, 04:04 PM
Try this:

<?php
$fp = fsockopen('translate.google.com', 80, $errNum, $err);
if (!$fp) {
echo "Error $errNum : $err <br>";
} else {
$fout = "POST http://translate.google.com/translate_t HTTP/1.1\r\n";
$fout .= "Content-Length: 56\r\n";
$fout .= "Content-Type: application/x-www-form-urlencoded\r\n\r\n";
$fout .= "hl=en&ie=UTF8&text=-->this+is+a+test<--&langpair=en%7Car";

fwrite($fp, $fout);
fclose($fp);

?>

web wheeler
12-18-2007, 06:38 PM
I tried this code and got pretty much the same results as above:

<?php
$http_response = '';
$fp = fsockopen('translate.google.com', 80, $errNum, $err);
if (!$fp) {
echo "Error $errNum : $err <br>";
} else {
$fout = "POST http://translate.google.com/translate_t HTTP/1.1\r\n";
$fout .= "Content-Length: 56\r\n";
$fout .= "Content-Type: application/x-www-form-urlencoded\r\n\r\n";
$fout .= "hl=en&ie=UTF8&text=-->this+is+a+test<--&langpair=en%7Car";
}
fwrite($fp, $fout);
while (!feof($fp))
{
$http_response .= fgets($fp, 128);
}
fclose($fp);
echo $http_response;
?>


Any other suggestions?

web wheeler
12-18-2007, 10:08 PM
I also tried using CURL code ( http://atv.bounceme.net/atvCommon/myTest2.php ), and I got the same results as with MY TEST:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns='http://www.w3.org/1999/xhtml' xml:lang='en' lang='en'>
<head>
<title>My Test 2</title>
<meta name='description' content='myTest2.php' />
<meta http-equiv='content-type' content='text/html; charset=UTF-8' />
</head>
<body>
<?php
$http_response = '';
$fp = curl_init('http://translate.google.com/translate_t');
curl_setopt($fp, CURLOPT_POST,1);
curl_setopt($fp, CURLOPT_POSTFIELDS,'hl=en&ie=UTF8&text=-->this+is+a+test<--&langpair=en%7Car');
curl_setopt($fp, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($fp, CURLOPT_HEADER,0);
curl_setopt($fp, CURLOPT_RETURNTRANSFER,1);
$http_response = curl_exec($fp);
echo $http_response;
?>
</body>
</html>


CURL is quite a bit faster, but it still doesn't fix the problem. This is really bugging me!

If anyone could help out I would sure appreciate it!