Click to See Complete Forum and Search --> : fsockopen / table error


dcjones
11-20-2003, 07:42 AM
Hi All.

This is my code:

<?php

$text = "I have a dog.";

translate($text, "en", "de");
exit;

function translate($text, $srclang, $trglang)
{
$request = "GET /S301.1/api?wl_data=";
#must always urlencode parameter values that contain whitespace or other special characters
$request .= urlencode($text);
$request .= "&wl_errorstyle=1";
$request .= "&wl_srclang=$srclang&wl_trglang=$trglang&wl_password=password HTTP/1.1\n";
$request .= "Accept-Charset: iso-8859-1\n";
$request .= "Host: www.mydomain.com\n";
$request .= "Connection: Keep-Alive\n\n";


$server = "www.mydomain.com";
$port = 80;
$socket = fsockopen($server, $port, &$errno, &$errstr);
fwrite($socket, $request);
echo "<html><body><table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
echo "<tr>";
echo "<td width=\"100%>";
while (!feof($socket))
{
$str = fgets($socket, 99999);
echo $str;
}

echo "</td>";
echo "</tr>";
echo "</table>";
fclose($socket);

echo "</body></html>";
print("\n");
}

?>

If I run the code the output is :

<td width="100%>HTTP/1.1 200 Date: Thu, 20 Nov 2003 13:37:25 GMT Server: Apache Set-Cookie: mydomain.com=212.227.119.39.1069335445040508; path=/; expires=Tue, 18-Nov-08 13:37:25 GMT Set-Cookie: JSESSIONID=DDC55000894AE3CC60CDE78A86E4C6E1.tc2; Path=/ X-WL-ERRORCODE: 0 Content-Type: text/plain;charset=UTF-8 Content-Length: 21 Keep-Alive: timeout=15, max=100 Connection: Keep-Alive Ich habe einen Hund.


What I am trying to do is make the output display in the table.

Anyone know what is wrong. If you do please, I am a new php person.

If you can help me please

Regards, keep safe and well.

Regards

Dereck

Kr|Z
11-20-2003, 04:36 PM
If you want only "Ich habe einen Hund." to be displayed, try changing these lines:


while (!feof($socket))
{
$str = fgets($socket, 99999);
echo $str;
}


to:


while (!feof($socket))
{
$str = fgets($socket, 99999);
}
$start = strpos($str, "\r\n\r\n");
echo(substr($str, $start));

dcjones
11-21-2003, 09:59 AM
Hi and thanks for your reply.

I have change the script to the following:

<?php

$text = "I have a dog.";

translate($text, "en", "de");
exit;

function translate($text, $srclang, $trglang)
{
$request = "GET /S301.1/api?wl_data=";
#must always urlencode parameter values that contain whitespace or other special characters
$request .= urlencode($text);
$request .= "&wl_errorstyle=1";
$request .= "&wl_srclang=$srclang&wl_trglang=$trglang&wl_password=password HTTP/1.1\n";
$request .= "Accept-Charset: iso-8859-1\n";
$request .= "Host: www.mydomain.com\n";
$request .= "Connection: Keep-Alive\n\n";


$server = "www.mydomain.com";
$port = 80;
$socket = fsockopen($server, $port, &$errno, &$errstr);
fwrite($socket, $request);
echo "<html><body><table width=\"100%\" border=\"0\" cellspacing=\"0\" cellpadding=\"0\">";
echo "<tr>";
echo "<td width=\"100%>";
while (!feof($socket))
{
$str = fgets($socket, 99999);
}
$start = strpos($str, "\r\n\r\n");
echo(substr($str, $start));

echo "</td>";
echo "</tr>";
echo "</table>";
fclose($socket);

echo "</body></html>";
print("\n");
}

?>

and the output to the screen is:

<td width="100%>


Any idea why, I do need help here, Thanks

Keep safe and well.

Regards

Dereck

Kr|Z
11-21-2003, 05:18 PM
Sorry, forgot a dot there. Change it to:


while (!feof($socket))
{
$str.= fgets($socket, 99999);
}
$start = strpos($str, "\r\n\r\n");
echo(substr($str, $start));