This is a strange one but I've been stuck on it for days. Any help
appreciated.
THE PLAN:
I've a database that I use a script to grab all the entries for a
particular field.
I then want to send each of these entries individually to another
server using fputs.
The server will send 1 response (a lin of text) back for each item
sent to it.
I'd like to capture these responses and store them in a table.
WHAT I HAVE:
I can send 1 item manually at a time and get a response back from the
remote server. The connections and sockets are all working. I can
print the list of entries I'm grabbing, ready to send to the server. I
can print the server response to the screen for a single manually sent
item.
So my problem is that I don't know how to send or loop the field
entries that I grab from my database 1 by 1 automatically and quickly,
and then capture the remote server responses - inserting them to a
table.
MY CODE SO FAR (notated where I've fudged things a little):
If anyone can help I would really appreciate it. Please reply if you
have the skills to solve this - it's been wasting my days :(
<?php
$fp = fsockopen ('remote-server-address.com', port, $errno, $errstr,
30);
if (!$fp)
{
echo "$errstr ($errno)<br />\n";
}
else
{
// get list of items from local database
$host="localhost"; // Host name
$username="localusername";
$password="localpassword";
$db_name="localdatabasename";
$db_con = mysql_connect("$host", "$username", "$password")or die
("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");
$query = "SELECT fieldname FROM tablename";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC))
{
$items = "{$row['domain']} <br>";
// prints a list of the items
// i don't need the script to do this, but just checking the contents
of the variable
echo $items;
}
// send and receive to and from remote server
// I really want to get rid of the first line below and just send
$item results directly
// into the $itementervariable
// and have the fputs run quickly sending each of the items to the
remote server and
// capture the server responses
// then eventually store each of the server responses into another
table.
$itementer = "testmessage";
fputs($fp, "$itementer\r\n");
$remote-server-response = fgets($fp, 128);
// prints the server response
// i don't need the script to do this, but just checking the contents
of the variable
// at the moment I'm specifying $itementer = testmessage so only a
single
// response is printed from the line below
// eventually need the list of many responses to be entered into a
table on the db
echo $remote-server-response;
fclose($fp);
}
mysql_close($db_con);
?>