note 26334 added to function.fopen

From: Date: Sat, 26 Oct 2002 21:41:14 +0000
Subject: note 26334 added to function.fopen
Groups: php.notes 
Request: Send a blank email to php-notes+get-38603@lists.php.net to get a copy of this message
I've found that some servers don't support HTTP/1.0 and will actually return a 403 error (forbidden). This is especially true with Google (my favorite search engine). So, in order to get around this limitation of the fopen command I wrote a routine to use HTTP/1.1 instead (i wrote a simple check for a 403 from fopen first, but this is the important piece)... <START CODE> $GrabURL = "/search"; $sockhandle = fsockopen("www.google.com", 80, &$errno, &$errstr); if(!$sockhandle) { print "server not available!"; } else { $request = "GET $GrabURL HTTP/1.1\r\n"; $request .= "User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows 98)\r\n"; $request .= "Host: www.google.com\r\n"; $request .= "Connection: Close\r\n\r\n"; fputs($sockhandle, $request); $line = fgets($sockhandle, 1024); //Request Method $line = fgets($sockhandle, 1024); //Close Method $line = fgets($sockhandle, 1024); //Server Type $line = fgets($sockhandle, 1024); //Date $line = fgets($sockhandle, 1024); //Transfer-Encoding $line = fgets($sockhandle, 1024); //Content-Type $line = fgets($sockhandle, 1024); //Cache-control $line = fgets($sockhandle, 1024); //Set-Cookie $line = fgets($sockhandle, 1024); //End Header "space" $content = ""; while (!feof($sockhandle)) { $line = fgets($sockhandle, 1024); //Packet length $length = hexdec($line); $content .= fread($sockhandle, $length); //Packet data } print $content; } fclose($sockhandle); <END CODE> If you have any questions feel free to email me, to see this code working in action visit http://google.compholio.com/. If you are curious as to why I did this it's because the custom google search doesn't show up properly if you set the links to be white, so I tacked on some HTML to turn the links the appropriate color but not screw everything else up. As of this posting my IP address was in the process of changing, so if you can't reach the site try again in a day or so. -- http://www.php.net/manual/en/function.fopen.php http://master.php.net/manage/user-notes.php?action=edit+26334 http://master.php.net/manage/user-notes.php?action=delete+26334 http://master.php.net/manage/user-notes.php?action=reject+26334

« previous php.notes (#38603) next »