Page 1 of 1
problem using fsockopen
Posted: Sun Dec 10, 2006 7:48 pm
by visonardo
Im starting to drive sockets and http header, i was testing this but i have a problem and i dont know why/where
i took like url example to devnetwork (tested with other and too dont work)
Code: Select all
<?php
$url='www.devnetwork.net';
$headers="GET / HTTP/1.0\r\n";
$da = fsockopen($url, 80);
if (!$da) {
echo "No fue posible abrir\n";
} else {
fwrite($da, $headers);
stream_set_timeout($da, 2);
$res = fread($da, 2000);
$info = stream_get_meta_data($da);
fclose($da);
echo '<pre>';
print_r($info);
echo '</pre>';
echo '<p>'.$res;
}
?>
what is bad here?
Posted: Sun Dec 10, 2006 11:34 pm
by Sloth
Could be your host is blocking outgoing traffic on certain ports. Some hosts do that
Try checking with 'em and let us know
Posted: Mon Dec 11, 2006 9:26 am
by visonardo
i guess that not, because i used/use snoopy class that use fsockopen and no problem, but i dont know why this doesnt.
Posted: Mon Dec 11, 2006 10:47 am
by feyd
I seem to remember needing to specify the host I was seeking.
Posted: Mon Dec 11, 2006 11:15 am
by visonardo
i solved that doing thus:
Code: Select all
$da = fsockopen("forums.devnetwork.net", 80, $errno, $errstr, 30);
if (!$da) {
echo "$errstr ($errno)<br />\n";
} else {
$salida = "GET / HTTP/1.0\r\n";
$salida .= "Host: forums.devnetwork.net\r\n";
$salida .= "Connection: Close\r\n\r\n";
fwrite($da, $salida);
$aver=array();
while (!feof($da)) {
echo fgets($da,128);
}
fclose($da);
}
but this print
Code: Select all
HTTP/1.1 200 OK Date: Mon, 11 Dec 2006 17:13:47 GMT
Server: Microsoft-IIS/5.0
X-Powered-By: PHP/4.4.2
Set-Cookie: phpbb2mysql_data=a%3A2%3A%7Bs%3A11%3A%22autologinid%22%3Bs%3A0%3A%22%22%3Bs%3A6%3A%22userid%22%3Bi%3A-1%3B%7D; expires=Tuesday, 11-Dec-07 17:13:47 GMT; path=/; domain=.devnetwork.net
Set-Cookie: phpbb2mysql_sid=09451a811bff64524c3d2e36c0864fb3; path=/; domain=.devnetwork.net Cache-Control: private, pre-check=0, post-check=0, max-age=0 Expires: 0 Pragma: no-cache Connection: close
Content-Type: text/html
how can i do to dont show that? it appear before html code, but suposedly it would not appear
and other thing, is needed "Connection: close" always?
Posted: Mon Dec 11, 2006 11:22 am
by feyd
visonardo wrote:how can i do to dont show that? it appear before html code, but suposedly it would not appear
What?
I'll guess that you mean "I don't want to show the headers." To which the response is, headers are denoted as the first paragraph of the returned information. So look for the first blank line. Once you find it, all before that is the headers. Everything afterward is the page content.
viewtopic.php?t=29312 may be of interest.
Posted: Mon Dec 11, 2006 11:47 am
by Chris Corbyn
Connection: close SHOULD be specified but doesn't NEED to be. It just tells the server it's OK to hang up once it's all done. there's generally no reason you'd want them not to and 99% of the time they will do anyway.
Headers are separated from the content by <CRLF><CRLF> so as soon as you fgets() a line which is just \r\n you can start cacthing the content into a string.
Note that you'll want to check for things like chunked encoding since your data will look odd if not
EDIT | I just noticed you're using HTTP 1.0 so ignore my comment about chunked encoding.
Posted: Mon Dec 11, 2006 6:19 pm
by Sloth
If you're just interested in getting the html output of a script why not just use file_get_contents() ?
Posted: Mon Dec 11, 2006 7:54 pm
by visonardo
Sloth wrote:If you're just interested in getting the html output of a script why not just use file_get_contents() ?
no, i need the headers obviously, but i dont need show that
by now all it is fixed, if anyproblem i will be tell you