469,271 Members | 1,743 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,271 developers. It's quick & easy.

GET Request w/fsockopen()

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
5 22703
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" <no****@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;

Jul 17 '05 #2
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" <no****@nowhere.com> wrote in message
news:dt********************@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" <no****@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;


Jul 17 '05 #3
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/
Jul 17 '05 #4
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" <no****@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" <no****@nowhere.com> wrote in message
news:dt********************@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" <no****@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;




Jul 17 '05 #5
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
Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Michael T. Peterson | last post: by
9 posts views Thread by fipaj1992 | last post: by
1 post views Thread by Syl | last post: by
2 posts views Thread by Alex | last post: by
2 posts views Thread by Vladimir Ghetau | last post: by
6 posts views Thread by 703designs | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.