problem using fsockopen

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
User avatar
visonardo
Forum Contributor
Posts: 136
Joined: Mon Oct 02, 2006 7:49 pm
Location: Argentina, South America´s Sun.

problem using fsockopen

Post 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?
Sloth
Forum Newbie
Posts: 18
Joined: Thu Dec 07, 2006 7:29 pm

Post 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
User avatar
visonardo
Forum Contributor
Posts: 136
Joined: Mon Oct 02, 2006 7:49 pm
Location: Argentina, South America´s Sun.

Post 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.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I seem to remember needing to specify the host I was seeking.
User avatar
visonardo
Forum Contributor
Posts: 136
Joined: Mon Oct 02, 2006 7:49 pm
Location: Argentina, South America´s Sun.

Post 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?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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.
Sloth
Forum Newbie
Posts: 18
Joined: Thu Dec 07, 2006 7:29 pm

Post by Sloth »

If you're just interested in getting the html output of a script why not just use file_get_contents() ?
User avatar
visonardo
Forum Contributor
Posts: 136
Joined: Mon Oct 02, 2006 7:49 pm
Location: Argentina, South America´s Sun.

Post 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 :wink:


by now all it is fixed, if anyproblem i will be tell you
Post Reply