Sending a GET request and getting the response using fsockopen.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Aetherweb

    #1

    Sending a GET request and getting the response using fsockopen.

    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?param 1=1&param2=2&pa ram3=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_time out($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

    #2
    Re: Sending a GET request and getting the response using fsockopen.

    On Tue, 30 Jan 2007 03:36:36 -0800, Aetherweb <jeffsnox@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?param 1=1&param2=2&pa ram3=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_time out($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

    Comment

    • Rik

      #3
      Re: Sending a GET request and getting the response using fsockopen.

      Aetherweb <jeffsnox@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?param 1=1&param2=2&pa ram3=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

      Comment

      • Aetherweb

        #4
        Re: Sending a GET request and getting the response using fsockopen.

        Sorted. Thank you.

        On Jan 30, 11:50 am, Rik <luiheidsgoe... @hotmail.comwro te:
        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?param 1=1&param2=2&pa ram3=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

        Comment

        Working...