note 61917 rejected from function.parse-url by didou

From: Date: Sat, 03 Feb 2007 03:06:52 +0000
Subject: note 61917 rejected from function.parse-url by didou
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-122611@lists.php.net to get a copy of this message
Note Submitter: Tyron Madlener ---- I needed to get information about the real URL, because some URL are just redirects to the real one. This function here tracks down the real URL as long as it gets redirects (errorcode 301,302, 303, 305 or 307) as response. In this example i resolve my homepage www.tyron.at which redirects to a subdirectory. $real_url = ResolveURL("http://www.tyron.at"); echo "<pre>"; print_r(parse_url($real_url)); function ResolveURL($url) { $info = parse_url($url); //echo "url: '$url'<br><pre>"; //print_r($info); $fp = fsockopen($info['host'],80); if($fp) { $path = $info['path']; if(!strlen($path)) $path = "/"; if(strlen($info['query'])) $path.="?".$info['query']; $out = "GET $path HTTP/1.1\r\n"; $out .= "Host: ".$info['host']."\r\n"; $out .= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.7.8) Gecko/20050511 Firefox/1.0.4\r\n"; $out .= "Connection: Close\r\n\r\n"; fwrite($fp, $out); if(ereg("HTTP/1.1 30(1|2|3|5|7)",$resp = fgets($fp,512))) { while (!feof($fp)) { if(preg_match("/Location: (.*)$/",$line=fgets($fp, 1024),$matches)) $url = ResolveURL(chop($matches[1])); } } //echo $resp; fclose($fp); } return $url; }

« previous php.notes (#122611) next »