note 46836 modified in function.fsockopen by danbrown
| From: | danbrown@php.net | Date: | Sun, 20 Jul 2008 19:49:05 +0000 |
| Subject: | note 46836 modified in function.fsockopen by danbrown | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-142313@lists.php.net to get a copy of this message | ||
The following snippet allows you to retrieve the title of a page.
Great for rewriting auto-url detectors to display the actual title rather then http://...
<?php
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];
}
}
?>
--was--
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];
}
}
?>
http://php.net/manual/en/function.fsockopen.php