Page 1 of 1

Send reponse to a client's POST

Posted: Thu Mar 19, 2009 1:08 pm
by Sunny Dee
Hi,

I have a php script that waits to receive a POST, and when it does it performs some manipulation of the data within the post's body, and returns a string to where the POST came from.

My code looks like:

Code: Select all

<?php
$host = gethostbyname($_SERVER['REMOTE_ADDR']);
$myPIN = $_POST['PIN'];
 
$key = "key=" . $myPIN;
 
$fp = fsockopen($host, 80);
 
fputs($fp, "HTTP/1.1 200 Ok\r\n");
fputs($fp,"Content-Type: application/www-url-encoded\r\n");
fputs($fp, "Content-Length: " . strlen($key) . "\r\n\r\n");
fputs($fp, $key);
 
fclose($fp);
 
?>
But this doesn't seem to work. I keep getting a timeout. ANy ideas? Is how I'm trying to send a response correct?

Thanks

Re: Send reponse to a client's POST

Posted: Thu Mar 19, 2009 1:15 pm
by crazycoders
What is the point of this operation? :dubious:

I don't understand why you connect to yourself and second why your request is formatted this way... You open a socket to yourself and don't ask for anything but you seem to be outputting something... really really strange.

Re: Send reponse to a client's POST

Posted: Thu Mar 19, 2009 1:21 pm
by Sunny Dee
I'm not connecting to myself.

I know that I will be receiving a POST with the format:

POST /pathfromdeveloper HTTP/1.1
Content-Type: application/www-url-encoded
Content-Length: 120
Host: hostfromdeveloper
PIN=12341234&email=customeremail@email.com&product=product&version=1.2&transact
ionid=123&test=false

I then need to generate a Key, which I do by manipulating the PIN element (I didn't show this in the previous post, because I can figure that part out).

The portal that sent the POST is expecting something returned with the following format:

HTTP/1.1 200 OK
Content-Type: application/www-url-encoded
Content-Length: 20
key=ABCDEFGHIJK

Does that clarify things?

Thanks.

Re: Send reponse to a client's POST

Posted: Thu Mar 19, 2009 1:40 pm
by crazycoders
Ok it's clearer now. Do you need to POST back to it or simply respond? Because if it's a simple response, then do the following:

Code: Select all

 
Header("HTTP/1.1 200 Ok");
Header("Content-Type: application/www-url-encoded");
Header("Content-Length: " . strlen($key));
echo $key;
 
This will return the information to the caller, in that case your portal that is calling you. If its more of a ping back that you need, then you need to setup a socket and call the page specified in the documentation. These scripts are the hardest to troubleshoot because you can't see what is happening unless you simulate the calling yourself...

Am i clearer? Did i understand your problem?

Re: Send reponse to a client's POST

Posted: Thu Mar 19, 2009 1:52 pm
by Sunny Dee
It's now receiving the 'key' properly now, thank you!!!

But the headers aren't being written. the headers being written are:

HTTP/1.1 200 OK
Content-Type: text/html
Content-Length: -1

I'm guessing that these are the default values my server is sending back. Any ideas on how to overwrite these?

Thanks a lot!!! :)

Re: Send reponse to a client's POST

Posted: Thu Mar 19, 2009 2:04 pm
by crazycoders
If you use the "header" function it will append them and then normal way for a browser or user agent to read the headers is to read sequentially and overwrite the values is they are repeated. So if you browser receives:

Code: Select all

 
HTTP/1.1 200 OK
Content-Type: text/html
Content-Length: -1
Content-Type: text/plain
Content-Length: 200
 
It will assume the content is text plain of 200 bytes, not text/html...

Cheers

Re: Send reponse to a client's POST

Posted: Thu Mar 19, 2009 2:15 pm
by Sunny Dee
Got it to work. I had whitespaces before my <?php start tag which was screwing things up.

THanks for the help!!!!