function read_title($url)
{
$url_info = parse_url($url);
if (!isset($url_info[‘host‘]) || !isset($url_info[‘scheme‘])) {
return false;
}
$host = $url_info[‘host‘];
$port = isset($url_info[‘port‘]) ? $url_info[‘port‘] : null;
$path = isset($url_info[‘path‘]) ? $url_info[‘path‘] : "/";
if(isset($url_info[‘query‘])) $path .= "?".$url_info[‘query‘];
if(empty($port)){
$port = 80;
}
if ($url_info[‘scheme‘] == ‘https‘){
$port = 443;
}
if ($url_info[‘scheme‘] == ‘http‘) {
$port = 80;
}
$out = "GET $path HTTP/1.1\r\n";
$out .= "Host: $host\r\n";
$out .= "User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.1.7)\r\n";
$out .= "Connection: Close\r\n\r\n";
$fp = fsockopen($host, $port, $errno, $errstr, 5);
if ($fp == NULL) {
error("get title from $url, error. $errno: $errstr \n");
return false;
}
fwrite($fp, $out);
$content = ‘‘;
while (!feof($fp)) {
$content .= fgets($fp, 1024);
if (preg_match("/<title>(.*?)<\/title>/is", $content, $matches)) {
fclose($fp);
return encode_to_utf8($matches[1]);
}
}
fclose($fp);
return false;
}
function encode_to_utf8($string)
{
return mb_convert_encoding($string, "UTF-8", mb_detect_encoding($string, "UTF-8, GB2312, ISO-8859-1", true));
}