note 81652 deleted from function.fsockopen by cmb

From: Date: Sun, 07 Jul 2019 07:51:36 +0000
Subject: note 81652 deleted from function.fsockopen by cmb
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-211492@lists.php.net to get a copy of this message
Note Submitter: dot dot dot dot dot alexander at gmail dot com ---- An edit to my below function for extra headers support and a bit of debugging ( array("key" => "value") type) <?php if(!function_exists("download")){ function download($uri, $port = 80, $extra_headers = NULL){ if(!function_exists("stripos")){ function stripos($str, $needle, $offset=0){ return strpos(strtolower($str),strtolower($needle),$offset); }/* endfunction stripos */ }/* endfunction exists stripos*/ if(!is_int($port))$port = 80; if(!is_array($extra_headers))$extra_headers = array(); $uri = strtr( strval($uri), array("http://" => "", "https://" => "ssl://", "ssl://" => "ssl://", "\\" => "/", "//" => "/") ); if( ( $protocol = stripos($uri, "://") ) !== FALSE ){ if( ( $domain_pos = stripos($uri, "/", ($protocol + 3)) ) !== FALSE ){ $domain = substr($uri, 0, $domain_pos); $file = substr($uri, $domain_pos); } else{ $domain = $uri; $file = "/"; } } else{ if( ( $domain_pos = stripos($uri, "/") ) !== FALSE ){ $domain = substr($uri, 0, $domain_pos); $file = substr($uri, $domain_pos); } else{ $domain = $uri; $file = "/"; } } $fp = fsockopen($domain, $port, $errno, $errstr, 30); if(!$fp){ return FALSE; } else{ $out = "GET " . $file . " HTTP/1.1\r\n"; $out .= "Host: " . $domain . "\r\n"; foreach( $extra_headers as $nm => $vl ){ $out .= strtr( strval($nm), array( "\r" => "", "\n" => "", ": " => "", ":" => "") ) . ": " . strtr( strval($vl), array( "\r" => "", "\n" => "", ": " => "", ":" => "") ) . "\r\n"; } $out .= "Connection: Close\r\n\r\n"; $response = ""; fwrite($fp, $out); while (!feof($fp)) { $response .= fgets($fp, 128); } fclose($fp); global $http_response_header; $http_response_header = array(); if( stripos($response, "\r\n\r\n") !== FALSE ){ $hc = explode("\r\n\r\n", $response); $headers = explode("\r\n", $hc[0]); if(!is_array($headers))$headers = array(); foreach($headers as $key => $header){ $a = ""; $b = ""; if( stripos($header, ":") !== FALSE ){ list($a, $b) = explode(":", $header); $http_response_header[trim($a)] = trim($b); } } return end($hc); } else if( stripos($response, "\r\n") !== FALSE ){ $headers = explode("\r\n", $response); if(!is_array($headers))$headers = array(); foreach($headers as $key => $header){ if( $key < ( count($headers) - 1 ) ){ $a = ""; $b = ""; if( stripos($header, ":") !== FALSE ){ list($a, $b) = explode(":", $header); $http_response_header[trim($a)] = trim($b); } } } return end($headers); } else{ return $response; } } }/*endfunction download*/ }/*endif no function download*/ ?>

« previous php.notes (#211492) next »