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 ...
|
|||||||
| FAQ | Members List | Calendar | Search | Today's Posts | Mark Forums Read |
|
|||
|
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; |
|
|||
|
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; > > |
|
|||
|
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; > > > > > > |
|
|||
|
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/ |
|
|||
|
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; > > > > > > > > > > > > |
|
|||
|
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 |