Connecting Tech Pros Worldwide Forums | Help | Site Map

[Q] Explaination of fsockopen() behavior

Michael T. Peterson
Guest
 
Posts: n/a
#1: Jul 17 '05
I'm having trouble understanding why fsockopen() returns null yet the
errstring/errno value yields:

"The operation completed successfully. (0)"

Here's the code (modified from an example found on many websites - google
"fsockopen php example")

<?php
$url =
'http://waterdata.usgs.gov/wa/nwis/uv?dd_cd=01&dd_cd=02&format=rdb&period=1&
site_no=12149000';
// $url = 'www.php.net';
$fp = fsockopen ($url, 80, $errno, $errstr, 30);
if (!$fp) {
echo "$errstr ($errno)<br>\n";
} else {
fputs ($fp, "GET / HTTP/1.1\r\nHost: ".$url."\r\n\r\n");
while (!feof($fp)) {
echo fgets ($fp,128);
}
fclose ($fp);
}
?>

Some further info: When $url = 'www.php.net', the requested page is
correctly returned. However, when I try to get the USGS page, $fp is 0 and
$errstr/$errno indicates everything is kosher.

What have I missed? How must I structure the url and/or fputs arguments to
correctly obtain this page?

Thanks, in advance,

Cheers

Michael


Daniel Tryba
Guest
 
Posts: n/a
#2: Jul 17 '05

re: [Q] Explaination of fsockopen() behavior


Michael T. Peterson <mtp1032@comcast.net> wrote:[color=blue]
> <?php
> $url = 'http://waterdata.usgs.gov/wa/nwis/uv?dd_cd=01&dd_cd=02&format=rdb&period=1&site_no=1 2149000';[/color]

This an url
[color=blue]
> // $url = 'www.php.net';[/color]

This is not an url but appears to be a hostname.
[color=blue]
> $fp = fsockopen ($url, 80, $errno, $errstr, 30);[/color]

First parameter to fsockopen needs to be be a hostname.

[snip][color=blue]
> Some further info: When $url = 'www.php.net', the requested page is
> correctly returned. However, when I try to get the USGS page, $fp is 0 and
> $errstr/$errno indicates everything is kosher.
>
> What have I missed? How must I structure the url and/or fputs arguments to
> correctly obtain this page?[/color]

It's quite obvious, the first $url example is not a hostname. you can
use parse_url to disect the url to get the hostname (for fsockopen) and
path+query (for in the fputs).

Or you could use eg file() instead.

--

Daniel Tryba

Closed Thread