GET Request w/fsockopen()

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

    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;


  • Han

    #2
    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.4832 73$cF.168086@rw crnsc53...[color=blue]
    > 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[/color]
    return[color=blue]
    > 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.
    >
    > --------------------------------------------------------------------------[/color]
    --[color=blue]
    > -
    >
    > $link = "http://www.domain.com? param1=string";
    >
    > $http_response = "";
    > $url = parse_url($link );
    > $fp = fsockopen($url[host], 80, $err_num, $err_msg, 30) or[/color]
    die("Socket-open[color=blue]
    > 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;
    >
    >[/color]


    Comment

    • Han

      #3
      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.3925 50$2x.131421@rw crnsc52.ops.asp .att.net...[color=blue]
      > 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.4832 73$cF.168086@rw crnsc53...[color=green]
      > > 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[/color]
      > return[color=green]
      > > 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.
      > >[/color]
      >
      > --------------------------------------------------------------------------
      > --[color=green]
      > > -
      > >
      > > $link = "http://www.domain.com? param1=string";
      > >
      > > $http_response = "";
      > > $url = parse_url($link );
      > > $fp = fsockopen($url[host], 80, $err_num, $err_msg, 30) or[/color]
      > die("Socket-open[color=green]
      > > 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;
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • Zurab Davitiani

        #4
        Re: GET Request w/fsockopen()

        Han wrote on Thursday 02 October 2003 21:23:
        [color=blue]
        > 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.[/color]

        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:

        Download ActiveLink PHP XML Package for free. ActiveLink PHP XML Package provides means to parse, read, modify and output XML and XML documents without using any PHP XML libraries. Included classes are: XML, XMLDocument, XMLBranch, XMLLeaf, RSS, Tag, Tree, Branch, Leaf, File, Socket, HTTPClient.


        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

        Comment

        • Han

          #5
          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.6729 61$uu5.110283@s ccrnsc04...[color=blue]
          > 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.3925 50$2x.131421@rw crnsc52.ops.asp .att.net...[color=green]
          > > 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.4832 73$cF.168086@rw crnsc53...[color=darkred]
          > > > The following function retreives a GET request. It works great with[/color][/color][/color]
          many[color=blue][color=green][color=darkred]
          > > > sites (exp. www.msn.com), but others just hang (exp. www.ebay.com) or[/color]
          > > return[color=darkred]
          > > > an error (exp. www.microsoft.com).
          > > >
          > > > I've tried adding additional headers like "Referer" and "Accept", but[/color][/color][/color]
          it[color=blue][color=green][color=darkred]
          > > > doesn't make a difference.
          > > >
          > > > What needs to be done to satisfy those web servers that won't[/color][/color][/color]
          cooperate?[color=blue][color=green][color=darkred]
          > > >
          > > > Thanks.
          > > >[/color]
          > >[/color]
          >
          > --------------------------------------------------------------------------[color=green]
          > > --[color=darkred]
          > > > -
          > > >
          > > > $link = "http://www.domain.com? param1=string";
          > > >
          > > > $http_response = "";
          > > > $url = parse_url($link );
          > > > $fp = fsockopen($url[host], 80, $err_num, $err_msg, 30) or[/color]
          > > die("Socket-open[color=darkred]
          > > > 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;
          > > >
          > > >[/color]
          > >
          > >[/color]
          >
          >[/color]



          Comment

          • Keith Bowes

            #6
            Re: GET Request w/fsockopen()

            Han wrote:[color=blue]
            > 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.
            >[/color]

            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


            Comment

            Working...