Re: [PHP] fsockopen and HTTP 1.1 Problem
--- Mooki <[EMAIL PROTECTED]> wrote: > I am using these to get the code of a page that is listed in the code > below. I am using the example code of fsockopen on php.net as > reference. > > I can get the response header but i have a problem with the CHUNKs. I can show you how to deal with chunked responses, but I think this is unnecessary. You can open a remote URL just as if it were a local file if allow_url_fopen is enabled: $handle = fopen('http://example.org/', 'r'); ... Hope that helps. Chris = Chris Shiflett - http://shiflett.org/ PHP Security - O'Reilly Coming Fall 2004 HTTP Developer's Handbook - Sams http://httphandbook.org/ PHP Community Site http://phpcommunity.org/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] fsockopen and HTTP 1.1 Problem
Hello Mooki, Thursday, March 25, 2004, 12:26:27 PM, you wrote: M> I am using these to get the code of a page that is listed in the code M> below. I am using the example code of fsockopen on php.net as reference. M> I can get the response header but i have a problem with the CHUNKs. You could save yourself a whole load of headaches by getting the free class file Snoopy, that will do exactly what you want (and a lot more) http://snoopy.sourceforge.com Then your code would just look like this: http://myplanetside.station.sony.com/outfit.jsp";; $submit_vars["outfitId"] = "6607"; $submit_vars["worldId"] = "21; $snoopy->submit($submit_url,$submit_vars); ?> Then the whole page is stored in $snoop->results, the headers in $snoopy->headers, etc etc. Lots of variations, see the doc files. :) -- Best regards, Richard Davey http://www.phpcommunity.org/wiki/296.html -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] fsockopen and HTTP 1.1 Problem
I am using these to get the code of a page that is listed in the code below. I am using the example code of fsockopen on php.net as reference. I can get the response header but i have a problem with the CHUNKs. the Code : $header = ""; $response = ""; if (!($fp = fsockopen ("myplanetside.station.sony.com", 80, $errno, $errstr,30))) exit($errstr); else { echo "bin drin "; echo "Bin richtig"; fputs ($fp, "GET /outfit.jsp?outfitId=6607&worldId=21 HTTP/1.1 \r\n"); fputs ($fp, "Host: myplanetside.station.sony.com\r\n"); fputs ($fp, "Connection: close\r\n"); fputs ($fp, "Accept-Encoding: gzip\r\n"); fputs ($fp, "Accept: application/x-shockwave-flash,text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,image/jpeg,image/gif;q=0.2,*/*;q=0.1\r\n"); fputs ($fp, "Accept-Language: en-us,en;q=0.5\r\n"); fputs ($fp, "Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7\r\n"); fputs ($fp, "Accept-Language: en-us,en;q=0.5\r\n\r\n"); do $header.=fread($fp,1); while (!preg_match('/\\r\\n\\r\\n$/',$header)); // check for chunked encoding if (preg_match('/Transfer\\-Encoding:\\s+chunked\\r\\n/',$header)){ echo "bin in der IF alls TRUE"; do { $byte = ""; $chunk_size=""; do { // Here should be the the Problem // I get endles query so to say. $chunk_size.=$byte; $byte=fread($fp,1); } while ($byte!="\\r"); // till we match the CR fread($fp, 1);// also drop off the LF $chunk_size=hexdec($chunk_size); // convert to real number $response.=fread($fp,$chunk_size); fread($fp,2); // ditch the CRLF that trails the chunk } while ($chunk_size); // till we reach the 0 length chunk (end marker) }//end if else { // check for specified content length if (preg_match('/Content\\-Length:\\s+([0-9]*)\\r\\n/',$header,$matches)) { $response=fread($fp,$matches[1]); } else { // not a nice way to do it (may also result in extra CRLF which trails the real content???) while (!feof($fp)) $response .= fread($fp, 4096); } } // close connection fclose($fp); } // do something useful with the response print($header); print($response); hope someone can help me ... P.S. : Sorry for the bad eng. mfG Munir -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] fsockopen and HTTP/1.1???
Alex Elderson wrote: >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. > This is because persistent TCP connections are the default behavior of HTTP/1.1. In HTTP/1.0, the client had to specifically request a persistent connection via the use of a "Connection: Keep-Alive" header in the HTTP request. Now (HTTP/1.1) it is quite the opposite; the client must specifically request for the server to close the connection with a "Connection: Close" header in the HTTP request. What is happening for you is this: 1) you use PHP socket functions to manually generate an HTTP request to a remote Web server 2) The Web server responds to your request and likely includes a "Connection: Close" header in the response. It does not, however, close the connection itself. It is simply letting you know that it is finished. 3) You do not parse the response or close the connection. You just sit there, and thus, nothing happens until a timeout condition is reached. You are basically implementing a primitive Web client, which is a very rewarding experience in terms of the education you provide yourself. It requires you, however, to deal with many details of socket programming. By leaving the socket open, you are able to send additional HTTP requests without all of the overhead of creating/destroying a socket for each transaction. This is extremely helpful, of course, for Web pages that include several images, so the GET requests for those images can be sent on the same connection. Additionally, you can now pipeline your requests; you do not have to wait for a response before issuing your next request. If you simply want to make a single request, receive the response, and have the connection be terminated, go ahead and include the "Connection: Close" header in your HTTP request, and this will request that the Web server close the connection for you. Otherwise, you'll need to do it for yourself. To close the connection yourself, you need to know when the server is finished sending, correct? You do this in two steps: 1) The header portion of the response is terminated by \r\n\r\n (though you should be prepared to accept just \n\n). 2) You read exactly how many bytes were specified in the "Content-Length" header and close the connection when you receive that. That's a basic introduction into the semantics of what you're getting into. If you still need more information, feel free to ask more questions; I enjoy explaining HTTP. Copy me on any responses if you want to ensure that I'll not miss your message. Cheers. Chris -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
[PHP] fsockopen and HTTP/1.1???
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 General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php