note 81652 added to function.fsockopen

From: Date: Sat, 08 Mar 2008 15:15:37 +0000
Subject: note 81652 added to function.fsockopen
Groups: php.notes 
Request: Send a blank email to php-notes+get-136836@lists.php.net to get a copy of this message
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*/ ?> ---- Server IP: 89.190.194.18 Probable Submitter: 87.120.68.77 ---- Manual Page -- http://www.php.net/manual/en/function.fsockopen.php Edit -- https://master.php.net/note/edit/81652 Del: integrated -- https://master.php.net/note/delete/81652/integrated Del: useless -- https://master.php.net/note/delete/81652/useless Del: bad code -- https://master.php.net/note/delete/81652/bad+code Del: spam -- https://master.php.net/note/delete/81652/spam Del: non-english -- https://master.php.net/note/delete/81652/non-english Del: in docs -- https://master.php.net/note/delete/81652/in+docs Del: other reasons-- https://master.php.net/note/delete/81652 Reject -- https://master.php.net/note/reject/81652 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#136836) next »