Hi,

I build a few functions to open webpages on the internet.

But it doesn't work very well, the first page is good the second page isn't good.

The output of this script will be somthing like this:


open connection to webapps.hardinxveld.netflex.nl oke

  get / done
  get / failt

Closing connection to webapps.hardinxveld.netflex.nl oke


The problem is the "Connection: Close\n\n" header. The webserver close the connection 
after the first request, if i remove 
the "Connection: Close\n\n" header the first fgets($web_conn, 128) command will never 
ends.

It's running on winXP apache 2.0.36 PHP 4.2.1 (test server) from the shell


What to do?

Alex Elderson




<?php

  $crlf = "\r\n";

  $address = "www.mp3.com";
  $url = "/";

  print "open connection to " . $address . " ";
  if (!$web_conn = web_open($address)) {
    print "failt" . $crlf . $crlf;
  } else {
    print "oke" . $crlf . $crlf;
    print "  get " . $url . " ";
    if ($data = web_read($web_conn, $address, $url)) {
      print "done" . $crlf;
    } else {
      print "failt" . $crlf;
    }
    print "  get " . $url . " ";
    if ($data = web_read($web_conn, $address, $url)) {
      print "done" . $crlf;
    } else {
      print "failt" . $crlf;
    }
    print "\nClosing connection from " . $address . " ";
    if (web_close($web_conn)) {
      print "oke" . $crlf . $crlf . $crlf;
    } else {
      print "failt" . $crlf . $crlf . $crlf;
    }
  }



  function web_open($address, $port = 80) {
    return fsockopen($address, $port);
  }

  function web_close($web_conn) {
    if ($web_conn) {
      return fclose($web_conn);
    } else {
      return FALSE;
    }
  }

  function web_read($web_conn, $address, $url = '/', $post = false, $cookie = false) {
    $ret = '';
    if ($web_conn) {
      /* Success.  Now check to see the type of request */
      if ($post) {
        $request = "POST $url HTTP/1.1\n";
        $request .= "Host: $address\n";
        $request .= "Content-Length: " . strlen( $post )."\n";
        $request .= "Content-Type:application/x-www-form-urlencoded\n";
        if ($cookie) $request .= "Cookie: $cookie\n";
        $request .= "Connection: Close\n\n";
        $request .= $post;
      } else {
        $request = "GET $url HTTP/1.1\n";
        $request .= "Host: $address\n";
        $request .= "Content-Type: text/html\n";
        if ($cookie) $request .= "Cookie: $cookie\n";
        $request .= "Connection: Close\n\n";
      }
      $return = '';

      /* Write the Request */
      fputs ($web_conn, $request);

      /* Get the response */
      while (!feof($web_conn)) {
        $ret .= fgets($web_conn, 128);
      }
   }
   return $ret;
 }


?>


-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php

Reply via email to