Newbie: how to read response from server output stream
Moderator: General Moderators
Newbie: how to read response from server output stream
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!!
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!!
Re: Newbie: how to read response from server output stream
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
- 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
Re: Newbie: how to read response from server output stream
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
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
Re: Newbie: how to read response from server output stream
Since I need to read back from the same connection, should I still add the "Connection: close" to my header?
K.
K.
Re: Newbie: how to read response from server output stream
If I add "Connection: close" to my header, I am back to the old empty html page response.
K.
K.
Re: Newbie: how to read response from server output stream
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?
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?
Re: Newbie: how to read response from server output stream
Hi tasairis!
Actually it's not homework LOL!
It's real work
I am a programmer all day but have offered to help with somebody's website to get a payment gateway going.
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.
Actually it's not homework LOL!
It's real work
I am a programmer all day but have offered to help with somebody's website to get a payment gateway going.
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.
Re: Newbie: how to read response from server output stream
Hmm... only thing I can think of is that the request method may be case-sensitive.
That is, uppercase "POST" and not lowercase "post".
That is, uppercase "POST" and not lowercase "post".
Re: Newbie: how to read response from server output stream
Still no luck.
Should I include "https://" in the server name?
Should I include "https://" in the server name?
Re: Newbie: how to read response from server output stream
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.)
Are you supposed to use a secure connection? (For a payment gateway you should, but that might not be in place yet.)
Re: Newbie: how to read response from server output stream
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.
Does that change things?
Everything is in place on their side. I just use specific test data in my XML.
Re: Newbie: how to read response from server output stream
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.
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.
Re: Newbie: how to read response from server output stream
It sounds like this is something they should mention in their API spec? ie which port to use!
Do you agree?
thanks again.
Do you agree?
thanks again.