home *** CD-ROM | disk | FTP | other *** search
/ Cricao de Sites - 650 Layouts Prontos / WebMasters.iso / Servidores / bb98.exe / SOhttp.php < prev    next >
PHP Script  |  2002-06-25  |  2KB  |  81 lines

  1. <?php if (!defined("SOhttp")) { define("SOhttp", 1);
  2.  
  3. //    Retrieve a web page.
  4. //    Inputs:
  5. //        $sURL: full URL of page to retrieve
  6. //        $sPage: returned page if successful
  7. //        $sUser: (optional) user to authenticate as
  8. //        $sPassword: (optional) password to authenticate as
  9. //    Outputs:
  10. //        $errmsg: empty if no error occurred, otherwise error message
  11. //
  12. function SOHTTPGet($sURL, &$sPage, $sUser = "", $sPassword = "") {
  13.     $errmsg = "";
  14.     $sPage = "";
  15.     do {
  16.  
  17.         //    Strip off prefix and port.
  18.         //
  19.         if (!(($nCursor = strpos($sURL, "http://")) === false)) {
  20.             $sURL = substr($sURL, 7);
  21.         }
  22.         if (($nCursor = strpos($sURL, '/')) > 0) {
  23.             $sServer = substr($sURL, 0, $nCursor);
  24.             $sURL = substr($sURL, $nCursor);
  25.         } else {
  26.             $sServer = $sURL;
  27.             $sURL = "/";
  28.         }
  29.         if (($nCursor = strpos($sServer, ':')) > 0) {
  30.             $nPort = substr($sServer, $nCursor + 1, 5);
  31.             // $nPort = settype($nPort, "integer");
  32.             $sServer = substr($sServer, 0, $nCursor);
  33.         } else {
  34.             $nPort = 80;
  35.         }
  36.  
  37.         //    Grab the page if we can...
  38.         //
  39.         $fp = fsockopen($sServer, $nPort, $nErr, $errmsg, 30);
  40.         if (!$fp) {
  41.             break;
  42.         }
  43.         if (strlen($sUser)) {
  44.             $sCreds = "Authorization: Basic ".base64_encode($sUser.":".$sPassword)."\r\n";
  45.         } else {
  46.             $sCreds = "";
  47.         }
  48.         $sReq = 
  49.             "GET ".$sURL." HTTP/1.0\r\n".
  50.             "User-Agent: Mozilla/4.0 (compatible; MSIE 4.01; Windows NT)\r\n".
  51.             "Accept-Encoding: none\r\n".
  52.             "Accept-Language: en-us,fr-be;q=0.5\r\n".
  53.             "Pragma: No-Cache\r\n".
  54.             "Host: ".$sServer."\r\n".
  55.             "Accept: */*\r\n".
  56.             $sCreds.
  57.             "\r\n";
  58.         fputs($fp, $sReq);
  59.         for ($i = 0; !feof($fp); $i++) {
  60.             $sTemp = fgets($fp, 16384);
  61.             if (!$i) {
  62.                 $nTemp = 555;
  63.                 if (($nCursor = strpos($sTemp, ' ')) > 0) {
  64.                     $nTemp = intval(substr($sTemp, $nCursor + 1));
  65.                 }
  66.                 if ($nTemp != 200) {
  67.                     $errmsg = substr($sTemp, $nCursor + 1);
  68.                     break;
  69.                 }
  70.             }
  71.             $sPage .= $sTemp;
  72.         }
  73.         fclose($fp);
  74.  
  75.     //
  76.     } while (0);
  77.     return ($errmsg);
  78. }
  79.  
  80. } ?>
  81.