GET Request w/fsockopen()

This is a discussion on GET Request w/fsockopen() within the PHP Language forums, part of the PHP Programming Forums category; The following function retreives a GET request. It works great with many sites (exp. www.msn.com), but others just ...


Go Back   Usenet Forums > PHP Programming Forums > PHP Language

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 10-02-2003
Han
 
Posts: n/a
Default GET Request w/fsockopen()

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;


Reply With Quote
  #2 (permalink)  
Old 10-03-2003
Han
 
Posts: n/a
Default Re: GET Request w/fsockopen()

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...
> 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;
>
>



Reply With Quote
  #3 (permalink)  
Old 10-03-2003
Han
 
Posts: n/a
Default Re: GET Request w/fsockopen()

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...
> 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...
> > 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;
> >
> >

>
>



Reply With Quote
  #4 (permalink)  
Old 10-03-2003
Zurab Davitiani
 
Posts: n/a
Default Re: GET Request w/fsockopen()

Han wrote on Thursday 02 October 2003 21:23:

> 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.


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/
Reply With Quote
  #5 (permalink)  
Old 10-03-2003
Han
 
Posts: n/a
Default Re: GET Request w/fsockopen()

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...
> 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...
> > 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...
> > > 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;
> > >
> > >

> >
> >

>
>




Reply With Quote
  #6 (permalink)  
Old 10-03-2003
Keith Bowes
 
Posts: n/a
Default Re: GET Request w/fsockopen()

Han wrote:
> 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.
>


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


Reply With Quote
Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On




All times are GMT +1. The time now is 06:43 AM.


Powered by vBulletin® Version 3.7.3
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0