Newbie: how to read response from server output stream

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
kimberly
Forum Commoner
Posts: 31
Joined: Tue Apr 21, 2009 12:48 pm

Newbie: how to read response from server output stream

Post by kimberly »

I am a programmer but very new to php and web programming.
I will be most grateful for your help!

I have been given vague instructions to:
"submit XML to the server, in a post parameter called xmlData.
Listen on the same connection, and wait for a response.
Once a response comes in, accept the xml response. This does not come in a
parameter, it is just written to the output stream on the original connection."

I believe I have done the request correctly, my problem is that I dont seem to get XML back, I just get an html page.

Here is my code:

$encodedString = urlencode($myString);
$server= 'www.someserver.com';
$url = '/something.jsp';
$content_length = strlen($encodedString);
$headers= "post $url HTTP/1.0\r\nContent-type: text/html\r\nHost: $server\r\nContent-length: $content_length\r\n\r\n";
$connection = fsockopen($server, 80, $errno, $errstr);
if (!$connection) return false;
fwrite($connection, $headers);
fwrite($connection, "xmlData=".$encodedString);
$result = "";
while (!feof($connection))
{
$result.= fread($connection, 1024);
}
fclose($connection);
print $result;

The result is the following html page:
<html>
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Set-Cookie: JSESSIONID=1FBA128B2301735A01680F36709AB497; Path=/vapp
Content-Type: text/html;charset=ISO-8859-1
Content-Length: 32
Date: Tue, 21 Apr 2009 17:45:48 GMT
</body>
</html>

I realise I will need to figure out how to parse the xml response, but I will worry about that later.
I just expect that I will be able to see the xml response as text first?
Am I doing something wrong, or is their response incorrect?
Should I include "https://" in the server name?

Many thanks in advance!!
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Newbie: how to read response from server output stream

Post by requinix »

Couple issues, maybe fixing them will get you a different response.

- The Content-Type isn't text/html. You aren't passing HTML. You're passing "application/x-www-form-urlencoded" data
- The Content-Length includes the "xmlData=" part. You aren't counting it right now
- You should tell the remote server to automatically close the connection. Add a "Connection: close" header
kimberly
Forum Commoner
Posts: 31
Joined: Tue Apr 21, 2009 12:48 pm

Re: Newbie: how to read response from server output stream

Post by kimberly »

Wow, thanks for the reply!
I changed the content type, corrected the content_length and I now get a response:
"The XML submitted was not received in the expected structure or format as laid out in the API"

I will double-check my XML. But you may hear from me again.

THANKS MILLIONS FOR YOUR HELP!

regards,
Kimberly
kimberly
Forum Commoner
Posts: 31
Joined: Tue Apr 21, 2009 12:48 pm

Re: Newbie: how to read response from server output stream

Post by kimberly »

Since I need to read back from the same connection, should I still add the "Connection: close" to my header?

K.
kimberly
Forum Commoner
Posts: 31
Joined: Tue Apr 21, 2009 12:48 pm

Re: Newbie: how to read response from server output stream

Post by kimberly »

If I add "Connection: close" to my header, I am back to the old empty html page response.

K.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Newbie: how to read response from server output stream

Post by requinix »

If you don't specify the Connection header, the server may keep the connection open after it's sent the data. So you can reuse the connection for another request.
Right now your code isn't prepared to do that. It keeps reading until the connection gets closed, which may be immediately or after a few seconds of no activity. Specifying Connection:close means the server will close the connection as soon as it responds.

The (homework?) specification only tells you to send a request and receive a response. Just one send/receive pair. Setting Connection:keep-alive (the opposite of "close") is for multiple requests.

What's your code right now?
kimberly
Forum Commoner
Posts: 31
Joined: Tue Apr 21, 2009 12:48 pm

Re: Newbie: how to read response from server output stream

Post by kimberly »

Hi tasairis!

Actually it's not homework LOL!
It's real work :banghead:
I am a programmer all day but have offered to help with somebody's website to get a payment gateway going. :crazy:

I definitely need only one request/response.

Thanks for your explanation of the connection close. If I change my headers to this:
$headers= "post $url HTTP/1.0\r\nContent-type: application/x-www-form-urlencoded\r\nHost: $server\r\nContent-length: $content_length\r\nConnection: close\r\n\r\n";

I get this:
<html>
HTTP/1.1 200 OK
Server: Apache-Coyote/1.1
Set-Cookie: JSESSIONID=C560F36CE513D608FDA857814BA9F61D; Path=/vapp
Content-Type: text/html;charset=ISO-8859-1
Date: Tue, 21 Apr 2009 19:41:49 GMT
Connection: close
</body>
</html>

I also changed this:
$content_length = strlen("xmlData=".$encodedString);
which seems to have helped

I sent my test XML through to them to make sure it is correct for testing purposes.

THANKS AGAIN FOR ALL YOUR HELP.

K.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Newbie: how to read response from server output stream

Post by requinix »

Hmm... only thing I can think of is that the request method may be case-sensitive.

That is, uppercase "POST" and not lowercase "post".
kimberly
Forum Commoner
Posts: 31
Joined: Tue Apr 21, 2009 12:48 pm

Re: Newbie: how to read response from server output stream

Post by kimberly »

Still no luck.
Should I include "https://" in the server name?
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Newbie: how to read response from server output stream

Post by requinix »

You don't add "https" anywhere, that's not how it works.

Are you supposed to use a secure connection? (For a payment gateway you should, but that might not be in place yet.)
kimberly
Forum Commoner
Posts: 31
Joined: Tue Apr 21, 2009 12:48 pm

Re: Newbie: how to read response from server output stream

Post by kimberly »

Yes, I imagine it will be a secure connection. The url has https
Does that change things?
Everything is in place on their side. I just use specific test data in my XML.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Newbie: how to read response from server output stream

Post by requinix »

Then it could be that you're supposed to use the secure connection. Which is on port 443, not on port 80.

Instead of using sockets to communicate, use cURL. Why? Because negotiating the secure connection yourself is a giant pain in the neck.

Take a look at the examples in the PHP manual and try to get something working for yourself.
kimberly
Forum Commoner
Posts: 31
Joined: Tue Apr 21, 2009 12:48 pm

Re: Newbie: how to read response from server output stream

Post by kimberly »

It sounds like this is something they should mention in their API spec? ie which port to use!
Do you agree?

thanks again.
Post Reply