This must have been done a thousand times!
It seems simple, but I am geting stumped.
I want a client script to lookup at another site before producing browser output.
The client script opens a socket and sends a request. Works. Check.
The server script receives the request and performs the lookup. Works. Check.
The server sends a response. Seems to work.
The client does not get it. Failed.
Not sure if server (2nd script below) should be sending with GET or using a different mime type, or if I am way out to lunch and doing something awful wrong. The response code is short and simple.
On the client side...
/////////////////////////////////////////////////////////
// www.client.com/client.php
// SITE CLIENT SENDS TO SITE HOME, REQUESTING LOOKUP FOR ID 3356
$request="do=lookup&id=3356";
$header='';
$header .= "POST /home.php HTTP/1.0\r\n";
$header .= "Host: www.home.com\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($request) . "\r\n\r\n";
$fp = fsockopen ('www.home.com', 80, $errno, $errstr, 45);
if (!$fp)
{
// HTTP ERROR LOGGING ETC
$res='';
}
else
{
// Post info, wait for response. Sending works. But response is empty.
fputs ($fp, $header . $req);
while (!feof($fp))
{
$res = fgets ($fp, 1024);
}
fclose ($fp);
flush();
}
// I NEVER GET THE RESPONSE HERE.
if($res='HAPPY')
{
// Good. I have a response. I am off to the races
// ...
}
else
{
// No response. I do something else.
// ...
}
// end of script. User sees some kind of response.
On the server side...
///////////////////////////////////////////////////////////////////////////////////
// www.home.com/home.php
// HOME SITE RECEIVES REQUEST AND REPLIES...
// Received posted request, did the lookup, set $reponse. All good so far. Now send response.
$response='HAPPY';
// THIS RESPONSE DOES NOT GET RECEIVED BY THE SCRIPT ABOVE
$header='';
$header .= "POST /client.php HTTP/1.0\r\n";
$header .= "Host: www.client.com\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($response) . "\r\n\r\n";
$fp = fsockopen ('www.client.com', 80, $errno, $errstr, 45);
if (!$fp)
{
// HTTP ERROR LOGGING ETC
}
else
{
// Reply and exit
fputs ($fp, $header . $response);
fclose ($fp);
flush();
}
// This script is completely hidden from the user. It is at a different domain and outputs nothing.
What am I missing ??!! Any assistance greatly appreciated. Thank you.