note 76899 modified in function.fopen by danbrown

From: Date: Mon, 08 Jun 2009 23:35:13 +0000
Subject: note 76899 modified in function.fopen by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-156547@lists.php.net to get a copy of this message
For those of you who do not have cURL, you might want to try this. It doesn't have all the functions that cURL has, but it has the basics. Please let me know of any bugs or problems. <?php function open_page($url,$f=1,$c=2,$r=0,$a=0,$cf=0,$pd=""){ global $oldheader; $url = str_replace("http://","",$url); if (preg_match("#/#","$url")){ $page = $url; $url = @explode("/",$url); $url = $url[0]; $page = str_replace($url,"",$page); if (!$page || $page == ""){ $page = "/"; } $ip = gethostbyname($url); }else{ $ip = gethostbyname($url); $page = "/"; } $open = fsockopen($ip, 80, $errno, $errstr, 60); if ($pd){ $send = "POST $page HTTP/1.0\r\n"; }else{ $send = "GET $page HTTP/1.0\r\n"; } $send .= "Host: $url\r\n"; if ($r){ $send .= "Referer: $r\r\n"; }else{ if ($_SERVER['HTTP_REFERER']){ $send .= "Referer: {$_SERVER['HTTP_REFERER']}\r\n"; } } if ($cf){ if (@file_exists($cf)){ $cookie = urldecode(@file_get_contents($cf)); if ($cookie){ $send .= "Cookie: $cookie\r\n"; $add = @fopen($cf,'w'); fwrite($add,""); fclose($add); } } } $send .= "Accept-Language: en-us, en;q=0.50\r\n"; if ($a){ $send .= "User-Agent: $a\r\n"; }else{ $send .= "User-Agent: {$_SERVER['HTTP_USER_AGENT']}\r\n"; } if ($pd){ $send .= "Content-Type: application/x-www-form-urlencoded\r\n"; $send .= "Content-Length: " .strlen($pd) ."\r\n\r\n"; $send .= $pd; }else{ $send .= "Connection: Close\r\n\r\n"; } fputs($open, $send); while (!feof($open)) { $return .= fgets($open, 4096); } fclose($open); $return = @explode("\r\n\r\n",$return,2); $header = $return[0]; if ($cf){ if (preg_match("/Set\-Cookie\: /i","$header")){ $cookie = @explode("Set-Cookie: ",$header,2); $cookie = $cookie[1]; $cookie = explode("\r",$cookie); $cookie = $cookie[0]; $cookie = str_replace("path=/","",$cookie[0]); $add = @fopen($cf,'a'); fwrite($add,$cookie,strlen($read)); fclose($add); } } if ($oldheader){ $header = "$oldheader<br /><br />\n$header"; } $header = str_replace("\n","<br />",$header); if ($return[1]){ $body = $return[1]; }else{ $body = ""; } if ($c === 2){ if ($body){ $return = $body; }else{ $return = $header; } } if ($c === 1){ $return = $header; } if ($c === 3){ $return = "$header$body"; } if ($f){ if (preg_match("/Location\:/","$header")){ $url = @explode("Location: ",$header); $url = $url[1]; $url = @explode("\r",$url); $url = $url[0]; $oldheader = str_replace("\r\n\r\n","",$header); $l = "&#76&#111&#99&#97&#116&#105&#111&#110&#58"; $oldheader = str_replace("Location:",$l,$oldheader); return open_page($url,$f,$c,$r,$a,$cf,$pd); }else{ return $return; } }else{ return $return; } } ///////////// ////Usage//// ///////////// $url = "http://www.php.net"; $f = 1; $c = 2;//1 for header, 2 for body, 3 for both $r = NULL; $a = NULL; $cf = NULL; $pd = NULL; $page = open_page($url,$f,$c,$r,$a,$cf,$pd); print $page; ?> --was-- For those of you who do not have cURL, you might want to try this. It doesn't have all the functions that cURL has, but it has the basics. Please let me know of any bugs or problems. <?php function open_page($url,$f=1,$c=2,$r=0,$a=0,$cf=0,$pd=""){ global $oldheader; $url = str_replace("http://","",$url); if (preg_match("#/#","$url")){ $page = $url; $url = @explode("/",$url); $url = $url[0]; $page = str_replace($url,"",$page); if (!$page || $page == ""){ $page = "/"; } $ip = gethostbyname($url); }else{ $ip = gethostbyname($url); $page = "/"; } $open = fsockopen($ip, 80, $errno, $errstr, 60); if ($pd){ $send = "POST $page HTTP/1.0\r\n"; }else{ $send = "GET $page HTTP/1.0\r\n"; } $send .= "Host: $url\r\n"; if ($r){ $send .= "Referer: $r\r\n"; }else{ if ($_SERVER['HTTP_REFERER']){ $send .= "Referer: {$_SERVER['HTTP_REFERER']}\r\n"; } } if ($cf){ if (@file_exists($cf)){ $cookie = urldecode(@file_get_contents($cf)); if ($cookie){ $send .= "Cookie: $cookie\r\n"; $add = @fopen($cf,'w'); fwrite($add,""); fclose($add); } } } $send .= "Accept-Language: en-us, en;q=0.50\r\n"; if ($a){ $send .= "User-Agent: $a\r\n"; }else{ $send .= "User-Agent: {$_SERVER['HTTP_USER_AGENT']}\r\n"; } if ($pd){ $send .= "Content-Type: application/x-www-form-urlencoded\r\n"; $send .= "Content-Length: " .strlen($pd) ."\r\n\r\n"; $send .= $pd; }else{ $send .= "Connection: Close\r\n\r\n"; } fputs($open, $send); while (!feof($open)) { $return .= fgets($open, 4096); } fclose($open); $return = @explode("\r\n\r\n",$return,2); $header = $return[0]; if ($cf){ if (preg_match("/Set\-Cookie\: /i","$header")){ $cookie = @explode("Set-Cookie: ",$header,2); $cookie = $cookie[1]; $cookie = explode("\r",$cookie); $cookie = $cookie[0]; $cookie = str_replace("path=/","",$cookie[0]); $add = @fopen($cf,'a'); fwrite($add,$cookie,strlen($read)); fclose($add); } } if ($oldheader){ $header = "$oldheader<br /><br />\n$header"; } $header = str_replace("\n","<br />",$header); if ($return[1]){ $body = $return[1]; }else{ $body = ""; } if ($c === 2){ if ($body){ $return = $body; }else{ $return = $header; } } if ($c === 1){ $return = $header; } if ($c === 3){ $return = "$header$body"; } if ($f){ if (preg_match("/Location\:/","$header")){ $url = @explode("Location: ",$header); $url = $url[1]; $url = @explode("\r",$url); $url = $url[0]; $oldheader = str_replace("\r\n\r\n","",$header); $l = "&#76&#111&#99&#97&#116&#105&#111&#110&#58"; $oldheader = str_replace("Location:",$l,$oldheader); return open_page($url,$f,$c,$r,$a,$cf,$pd); }else{ return $return; } }else{ return $return; } } ///////////// ////Usage//// ///////////// $url = "http://www.php.net"; $f = 1; $c = 2;//1 for header, 2 for body, 3 for both $r = NULL; $a = NULL; $cf = NULL; $pd = NULL; $page = open_page($url,$f,$c,$r,$a,$cf,$pd); print $page; ?> http://php.net/manual/en/function.fopen.php

« previous php.notes (#156547) next »