363,924 Members | 2585 Browsing Online
Community for Developers & IT Professionals
Bytes IT Community

GET Request w/fsockopen()

Han
P: n/a
Han
The following function retreives a GET request. It works great with many
sites (exp. www.msn.com), but others just hang (exp. www.ebay.com) or return
an error (exp. www.microsoft.com).

I've tried adding additional headers like "Referer" and "Accept", but it
doesn't make a difference.

What needs to be done to satisfy those web servers that won't cooperate?

Thanks.

----------------------------------------------------------------------------
-

$link = "http://www.domain.com?param1=string";

$http_response = "";
$url = parse_url($link);
$fp = fsockopen($url[host], 80, $err_num, $err_msg, 30) or die("Socket-open
failed--error: ".$err_num." ".$err_msg);
fputs($fp, "GET /$url[path]?$url[query] HTTP/1.0\n");
fputs($fp, "Connection: close\n\n");
while(!feof($fp)) {
$http_response .= fgets($fp, 128);
}
fclose($fp);
echo $http_response;


Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


Han
P: n/a
Han
Ok, I think part of the problem is that my function is being called from
within a frame. The path and host references are wrong. I might have to
reconsider where the request originates...

"Han" <nobody@nowhere.com> wrote in message
news:dy0fb.483273$cF.168086@rwcrnsc53...[color=blue]
> The following function retreives a GET request. It works great with many
> sites (exp. www.msn.com), but others just hang (exp. www.ebay.com) or[/color]
return[color=blue]
> an error (exp. www.microsoft.com).
>
> I've tried adding additional headers like "Referer" and "Accept", but it
> doesn't make a difference.
>
> What needs to be done to satisfy those web servers that won't cooperate?
>
> Thanks.
>
> --------------------------------------------------------------------------[/color]
--[color=blue]
> -
>
> $link = "http://www.domain.com?param1=string";
>
> $http_response = "";
> $url = parse_url($link);
> $fp = fsockopen($url[host], 80, $err_num, $err_msg, 30) or[/color]
die("Socket-open[color=blue]
> failed--error: ".$err_num." ".$err_msg);
> fputs($fp, "GET /$url[path]?$url[query] HTTP/1.0\n");
> fputs($fp, "Connection: close\n\n");
> while(!feof($fp)) {
> $http_response .= fgets($fp, 128);
> }
> fclose($fp);
> echo $http_response;
>
>[/color]


Jul 17 '05 #2

Han
P: n/a
Han
Ok, it seems the problem is related to the HTTP version. 1.0 works great,
but 1.1 complains about a missing host header. I added a host, but I still
get the same message.

"Han" <nobody@nowhere.com> wrote in message
news:dt4fb.392550$2x.131421@rwcrnsc52.ops.asp.att. net...[color=blue]
> Ok, I think part of the problem is that my function is being called from
> within a frame. The path and host references are wrong. I might have to
> reconsider where the request originates...
>
> "Han" <nobody@nowhere.com> wrote in message
> news:dy0fb.483273$cF.168086@rwcrnsc53...[color=green]
> > The following function retreives a GET request. It works great with many
> > sites (exp. www.msn.com), but others just hang (exp. www.ebay.com) or[/color]
> return[color=green]
> > an error (exp. www.microsoft.com).
> >
> > I've tried adding additional headers like "Referer" and "Accept", but it
> > doesn't make a difference.
> >
> > What needs to be done to satisfy those web servers that won't cooperate?
> >
> > Thanks.
> >[/color]
>
> --------------------------------------------------------------------------
> --[color=green]
> > -
> >
> > $link = "http://www.domain.com?param1=string";
> >
> > $http_response = "";
> > $url = parse_url($link);
> > $fp = fsockopen($url[host], 80, $err_num, $err_msg, 30) or[/color]
> die("Socket-open[color=green]
> > failed--error: ".$err_num." ".$err_msg);
> > fputs($fp, "GET /$url[path]?$url[query] HTTP/1.0\n");
> > fputs($fp, "Connection: close\n\n");
> > while(!feof($fp)) {
> > $http_response .= fgets($fp, 128);
> > }
> > fclose($fp);
> > echo $http_response;
> >
> >[/color]
>
>[/color]


Jul 17 '05 #3

Zurab Davitiani
P: n/a
Zurab Davitiani
Han wrote on Thursday 02 October 2003 21:23:
[color=blue]
> Ok, it seems the problem is related to the HTTP version. 1.0 works great,
> but 1.1 complains about a missing host header. I added a host, but I still
> get the same message.[/color]

This is not a direct answer to your question, but if you would like to see a
working HTTPClient implementation, get my ActiveLink PHP XML Package from:

http://php-xml.sourceforge.net/

and take a look at HTTPClient and Socket classes. They are somewhat limited
in functionality right now (it's their 1st release) but a simple GET
request should work with both 1.0 and 1.1 version of the protocol.

Good luck!

--
Business Web Solutions
ActiveLink, LLC
www.active-link.com/intranet/
Jul 17 '05 #4

Han
P: n/a
Han
Ok, fixed the problem.

Just in case this happens to someone else, here's the answer to my original
delimma.

Web servers are very particular about the number of line feeds and carriage
returns that follow headers. Some need "\r\n" and others "\r\n\r\n". Also,
you need to include a host header to make HTTP 1.1 servers happy. 1.0
servers don't require this and will ignore it.

"Han" <nobody@nowhere.com> wrote in message
news:KQ6fb.672961$uu5.110283@sccrnsc04...[color=blue]
> Ok, it seems the problem is related to the HTTP version. 1.0 works great,
> but 1.1 complains about a missing host header. I added a host, but I still
> get the same message.
>
> "Han" <nobody@nowhere.com> wrote in message
> news:dt4fb.392550$2x.131421@rwcrnsc52.ops.asp.att. net...[color=green]
> > Ok, I think part of the problem is that my function is being called from
> > within a frame. The path and host references are wrong. I might have to
> > reconsider where the request originates...
> >
> > "Han" <nobody@nowhere.com> wrote in message
> > news:dy0fb.483273$cF.168086@rwcrnsc53...[color=darkred]
> > > The following function retreives a GET request. It works great with[/color][/color][/color]
many[color=blue][color=green][color=darkred]
> > > sites (exp. www.msn.com), but others just hang (exp. www.ebay.com) or[/color]
> > return[color=darkred]
> > > an error (exp. www.microsoft.com).
> > >
> > > I've tried adding additional headers like "Referer" and "Accept", but[/color][/color][/color]
it[color=blue][color=green][color=darkred]
> > > doesn't make a difference.
> > >
> > > What needs to be done to satisfy those web servers that won't[/color][/color][/color]
cooperate?[color=blue][color=green][color=darkred]
> > >
> > > Thanks.
> > >[/color]
> >[/color]
>
> --------------------------------------------------------------------------[color=green]
> > --[color=darkred]
> > > -
> > >
> > > $link = "http://www.domain.com?param1=string";
> > >
> > > $http_response = "";
> > > $url = parse_url($link);
> > > $fp = fsockopen($url[host], 80, $err_num, $err_msg, 30) or[/color]
> > die("Socket-open[color=darkred]
> > > failed--error: ".$err_num." ".$err_msg);
> > > fputs($fp, "GET /$url[path]?$url[query] HTTP/1.0\n");
> > > fputs($fp, "Connection: close\n\n");
> > > while(!feof($fp)) {
> > > $http_response .= fgets($fp, 128);
> > > }
> > > fclose($fp);
> > > echo $http_response;
> > >
> > >[/color]
> >
> >[/color]
>
>[/color]



Jul 17 '05 #5

Keith Bowes
P: n/a
Keith Bowes
Han wrote:[color=blue]
> Ok, fixed the problem.
>
> Just in case this happens to someone else, here's the answer to my original
> delimma.
>
> Web servers are very particular about the number of line feeds and carriage
> returns that follow headers. Some need "\r\n" and others "\r\n\r\n". Also,
> you need to include a host header to make HTTP 1.1 servers happy. 1.0
> servers don't require this and will ignore it.
>[/color]

Not to sound like a jerk, but you really should read the spec. All
lines must end with \r\n. Plus, the final line must end with another
\r\n. So, your request should be:
GET /?param1=string HTTP/1.1\r\n
Host: www.domain.com\r\n
Connection: close\r\n\r\n


Jul 17 '05 #6

Post your reply

Help answer this question



Didn't find the answer to your PHP question?

You can also browse similar questions: PHP