note 46836 added to function.fsockopen
| From: | michiel at parse dot nl | Date: | Mon, 25 Oct 2004 11:42:00 +0000 |
| Subject: | note 46836 added to function.fsockopen | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-79069@lists.php.net to get a copy of this message | ||
The following snippet allows you to retrieve the title oa page.
Great for rewriting auto-url detectors to display the actual title rather then http://...
<?
echo get_url_title("http://www.php.net/cal.php?id=409");
function get_url_title($url, $timeout = 2)
{
$url = parse_url($url);
if(!in_array($url['scheme'],array('','http')))
return;
$fp = fsockopen ($url['host'], ($url['port'] > 0 ? $url['port'] :
80), $errno, $errstr, $timeout);
if (!$fp)
{
return;
// echo "$errstr ($errno)<br>\n";
}
else
{
fputs ($fp, "GET /".$url['path'].($url['query'] ?
'?'.$url['query'] : '')." HTTP/1.0\r\nHost:
".$url['host']."\r\n\r\n");
$d = '';
while (!feof($fp))
{
$d .= fgets ($fp,2048);
if(preg_match('~(</head>|<body>|(<title>\s*(.*?)\s*</title>))~i',
$d, $m))
break;
}
fclose ($fp);
return $m[3];
}
}
?>
----
Manual Page -- http://www.php.net/manual/en/function.fsockopen.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+46836
Delete -- http://master.php.net/manage/user-notes.php?action=delete+46836&report=yes
Reason: bad code -- http://master.php.net/manage/user-notes.php?action=delete+46836&report=yes&reason=bad+code
Reason: spam -- http://master.php.net/manage/user-notes.php?action=delete+46836&report=yes&reason=spam
Reason: useless example -- http://master.php.net/manage/user-notes.php?action=delete+46836&report=yes&reason=useless+example
Reason: contains commercial links -- http://master.php.net/manage/user-notes.php?action=delete+46836&report=yes&reason=contains+commercial+links
Reason: useless note -- http://master.php.net/manage/user-notes.php?action=delete+46836&report=yes&reason=useless+note
Reject -- http://master.php.net/manage/user-notes.php?action=reject+46836&report=yes
Search -- http://master.php.net/manage/user-notes.php