ヘッダ内のmetaはPHPに「get_meta_tags」という便利な関数があったので、以下のコードで簡単に取得可能。
$url = "http://example.com";
$meta = get_meta_tags($url);
上記だと、以下のように$metaにmetaタグの数だけ連想配列として格納される。
次にタイトル(title)は、以下のようにcURLでページの情報を取得して、そこから正規表現で抜き出す方法をとる。(参考=OKWAVE)
$url = "http://example.com";
$title = getPageTitle($url);
function getPageTitle($url){
static $regex = '@<title>([^<]++)</title>@i';
static $order = 'ASCII,JIS,UTF-8,CP51932,SJIS-win';
static $ch;
if(!$ch){
$ch = curl_init();
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
}
curl_setopt($ch, CURLOPT_URL, $url);
$html = mb_convert_encoding(curl_exec($ch), 'UTF-8', $order);
return preg_match($regex, $html, $m) ? $m[1] : '';
}
上記だと、$titleにタイトル文字列が変数として格納される。
コメント