By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,049 Members | 1,308 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,049 IT Pros & Developers. It's quick & easy.

Sending a GET request and getting the response using fsockopen.

P: n/a
Hi,

I need to use fsockopen to open a connection, send a GET request and
read in the response. At the moment I'm trying the following code, but
I think it's ignoring the actual URL included in the $request
variable.

$host = "www.myhost.com";
$request = "/myurl.php?param1=1&param2=2&param3=3";

$fp = fsockopen($host, 80, $errno, $errstr, 3.0);

if ($fp)
{
# Send the get request then, with timeout...
fwrite($fp, "GET / HTTP/1.0\r\n" .
"Host: $host\r\n".
"Connection: close\r\n".
"Content-Length: " . strlen($request) . "\r\n" .
"\r\n" .
$request);

stream_set_timeout($fp, 2, 0);

$response = '';
while (!feof($fp))
{
$response .= fread($fp, 1024);
}

print $response . "<BR>\n";
fclose($fp);
}
What am I doing wrong?

Thank you!!

Jan 30 '07 #1
Share this Question
Share on Google+
3 Replies


P: n/a
On Tue, 30 Jan 2007 03:36:36 -0800, Aetherweb <je******@gmail.comwrote:
Hi,

I need to use fsockopen to open a connection, send a GET request and
read in the response. At the moment I'm trying the following code, but
I think it's ignoring the actual URL included in the $request
variable.

$host = "www.myhost.com";
$request = "/myurl.php?param1=1&param2=2&param3=3";

$fp = fsockopen($host, 80, $errno, $errstr, 3.0);

if ($fp)
{
# Send the get request then, with timeout...
fwrite($fp, "GET / HTTP/1.0\r\n" .
"Host: $host\r\n".
"Connection: close\r\n".
"Content-Length: " . strlen($request) . "\r\n" .
"\r\n" .
$request);
The first line of the GET request needs to include the query string, as
far as I know. Instead of using "GET / HTTP/1.0\r\n", try inserting
$request in place of / after GET.
>
stream_set_timeout($fp, 2, 0);

$response = '';
while (!feof($fp))
{
$response .= fread($fp, 1024);
}

print $response . "<BR>\n";
fclose($fp);
}
What am I doing wrong?

Thank you!!
--
Curtis
Jan 30 '07 #2

P: n/a
Rik
Aetherweb <je******@gmail.comwrote:
Hi,

I need to use fsockopen to open a connection, send a GET request and
read in the response. At the moment I'm trying the following code, but
I think it's ignoring the actual URL included in the $request
variable.

$host = "www.myhost.com";
$request = "/myurl.php?param1=1&param2=2&param3=3";

$fp = fsockopen($host, 80, $errno, $errstr, 3.0);

if ($fp)
{
# Send the get request then, with timeout...
fwrite($fp, "GET / HTTP/1.0\r\n" .
"GET $request HTTP/1.0\r\n" .

--
Rik Wasmus
Jan 30 '07 #3

P: n/a
Sorted. Thank you.

On Jan 30, 11:50 am, Rik <luiheidsgoe...@hotmail.comwrote:
Aetherweb <jeffs...@gmail.comwrote:
Hi,
I need to use fsockopen to open a connection, send a GET request and
read in the response. At the moment I'm trying the following code, but
I think it's ignoring the actual URL included in the $request
variable.
$host = "www.myhost.com";
$request = "/myurl.php?param1=1&param2=2&param3=3";
$fp = fsockopen($host, 80, $errno, $errstr, 3.0);
if ($fp)
{
# Send the get request then, with timeout...
fwrite($fp, "GET / HTTP/1.0\r\n" ."GET $request HTTP/1.0\r\n" .

--
Rik Wasmus
Jan 30 '07 #4

This discussion thread is closed

Replies have been disabled for this discussion.