homepage Welcome to WebmasterWorld Guest from 54.234.2.94
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member
Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
Forum Library, Charter, Moderators: coopster & jatar k

PHP Server Side Scripting Forum

    
Single form posted to multiple locations
Any info sources?
willybfriendly




msg:1262256
 10:01 pm on Oct 13, 2004 (gmt 0)

I have a need to take one form, and post information from it to two different locations. Alternately, it could go to one location where data would be stored in a DB, but then some of that information needs to be forwarded on to another location.

Now, it looks like this is a problem that would be solved with sockets. I am confused about how to create the header information though.

Is anyone aware of a good source of info, or a tutorial that would help me to get my head around this? I have not had much luck searching...

Thanks,

WBF

 

Birdman




msg:1262257
 11:30 pm on Oct 13, 2004 (gmt 0)

Here is an example of posting a form with PHP using fsockopen. This code would reside in the script that the form is originally posted to.


function post($host, $path, $data) {
$http_response = '';
$content_length = strlen($data);
$fp = fsockopen($host, 80);
fputs($fp, "POST $path HTTP/1.1\r\n");
fputs($fp, "Host: $host\r\n");
fputs($fp, "Content-Type: application/x-www-form-urlencoded\r\n");
fputs($fp, "Content-Length: $content_length\r\n");
fputs($fp, "Connection: close\r\n\r\n");
fputs($fp, $data);
while (!feof($fp)) $http_response .= fgets($fp, 28);
fclose($fp);
return $http_response;
}

$postdata = '?foo=bar';
foreach($_POST as $key => $val) $postdata .= '&'.$key.'='.$val;

$http_response = post('example.org', '/path/to/script.php', $postdata);
?>

$http_response will return true or false.

Have fun!

[edited by: coopster at 10:14 pm (utc) on Mar. 4, 2005]
[edit reason] fixed typo [/edit]

willybfriendly




msg:1262258
 5:07 am on Oct 14, 2004 (gmt 0)

Thanks Birdman. Now can you help me to understand what I am seeing here? Please correct my comments:

function post($host, $path, $data) {
$http_response = '';//initialize variable
$content_length = strlen($data); //declare size of POST variable strings
$fp = fsockopen($host, 80); //connect to host domain, port 80
//begin http headers?
fputs($fp, "POST $path HTTP/1.1\r\n");
fputs($fp, "Host: $host\r\n");
fputs($fp, "Content-Type: application/x-www-form-rlencoded\r\n");
fputs($fp, "Content-Length: $content_length\r\n");
fputs($fp, "Connection: close\r\n\r\n");//what are we closing here?
//end http headers?
fputs($fp, $data); //transfering the POST data
while (!feof($fp)) $http_response .= fgets($fp, 28);//what are we doing on this line?
fclose($fp); //closing the socket connection
return $http_response; //How/why does this get set true false in the while loop?
}

$postdata = '?foo=bar'; //necessary to initialize this variable? Or just use $_POST..
foreach($_POST as $key => $val) $postdata .= '&'.$key.'='.$val; //..as here?

$http_response = post('example.org', '/path/to/script.php', $postdata); //I recognize a function call :)
?>

I want to understand what I am doing and why. Really do appreciate the help here.

WBF

Birdman




msg:1262259
 11:00 am on Oct 14, 2004 (gmt 0)

Hello again,

You figured most of it out :)

while (!feof($fp)) $http_response .= fgets($fp, 28);//This gets the output of the script, but only 28 bytes. You can test it to see if it was successful. if(!$http_response) print "Failed!";

return $http_response; //How/why does this get set true false in the while loop?
}

$postdata = '?foo=bar'; //I do this just to start the query string with the? and then we can simply use & in the for loop. The var is useless but it doesn't hurt anything.

foreach($_POST as $key => $val) $postdata .= '&'.$key.'='.$val; //now here we just append all vars with the &

You may want to double check the $http_response part. I can't remember if it's true/false or not. If it's just string data, test it with isset() and empty()

I'm definately not an expert on this so toy around with it and see what it does.

Regards,
Birdman

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / PHP Server Side Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved