私はcodeigniterを使用しています。使用しているphpフレームワークは関係ないと思います。
しかし、私が自分のクラスを書く前に、ユーザーが任意の場所のページタイトルとメタタグ(キーワード、説明)を取得できるようにする、すでに作成されている別のクラスがあります...もしあれば。
それを行うあらゆる種類のPHPクラスは素晴らしいでしょう。
皆さんありがとう
私はcodeigniterを使用しています。使用しているphpフレームワークは関係ないと思います。
しかし、私が自分のクラスを書く前に、ユーザーが任意の場所のページタイトルとメタタグ(キーワード、説明)を取得できるようにする、すでに作成されている別のクラスがあります...もしあれば。
それを行うあらゆる種類のPHPクラスは素晴らしいでしょう。
皆さんありがとう
このクラスを確認する必要があります。PHPSimpleHTMLDOMこのように機能します。
include('simple_html_dom.php');
$html = file_get_html('http://www.codeigniter.com/');
echo $html->find('title', 0)->innertext; // get <title>
echo "<pre>";
foreach($html->find('meta') as $element)
echo $element->name . " : " . $element->content . '<br>'; //prints every META tag
echo "</pre>";
PHPのcurlライブラリを使用します。Webから他のページをプルして文字列としてフェッチし、正規表現を使用して文字列を解析して、ページのタイトルとメタタグを見つけることができます。
get_meta_tagsを使用してリモートページからすべてのメタタグを取得できます-http ://ca3.php.net/get_meta_tags
このページには、ページと説明を取得するためのクラスがあり、get_meta_tagsも使用しています-http ://www.emirplicanic.com/php/get-remote-page-title-with-php.php
両方のビットを組み合わせて、必要なものをすべて取得できるはずです。
DOM/xpathを使用
libxml_use_internal_errors(true);
$c = file_get_contents("http://url/here");
$d = new DomDocument();
$d->loadHTML($c);
$xp = new domxpath($d);
foreach ($xp->query("//meta[@name='keywords']") as $el) {
echo $el->getAttribute("content");
}
foreach ($xp->query("//meta[@name='description']") as $el) {
echo $el->getAttribute("content");
}
これを見てください。これは、ページメタタグを取得してさらに多くのことを行うためのジェネリッククラスです。これをcodeigniterライブラリに追加できるかどうかを確認してください。ありがとう
これを試して:
libxml_use_internal_errors(true);
$urlDecoded = $this->input->post('url');
$c = file_get_contents($urlDecoded);
$d = new DomDocument();
$d->loadHTML($c);
$metaTags = [
'title' => '',
'description' => '',
'image' => '',
'canonical' => '',
'url' => '',
'author' => '',
'availability' => '',
'keywords' => '',
'og:description' => '',
'og:determiner' => '',
'og:image' => '',
'og:image:height' => '',
'og:image:secure_url' => '',
'og:image:type' => '',
'og:image:width' => '',
'og:locale' => '',
'og:locale:alternate' => '',
'og:site_name' => '',
'og:title' => '',
'og:type' => '',
'og:url' => '',
'price' => '',
'priceCurrency' => '',
'source' => '',
];
foreach ($d->getElementsByTagName('meta') as $meta) {
$property = $meta->getAttribute('property');
$content = $meta->getAttribute('content');
if (strpos($property, 'og') === 0) {
$metaTags[$property] = $content;
if ($property === 'og:title') $metaTags['title'] = $property;
if ($property === 'og:description') $metaTags['description'] = $property;
if ($property === 'og:image') $metaTags['image'] = $property;
}
}
$metaTags['canonical'] = $urlDecoded;
$metaTags['url'] = $urlDecoded;