2

私はcodeigniterを使用しています。使用しているphpフレームワークは関係ないと思います。

しかし、私が自分のクラスを書く前に、ユーザーが任意の場所のページタイトルとメタタグ(キーワード、説明)を取得できるようにする、すでに作成されている別のクラスがあります...もしあれば。

それを行うあらゆる種類のPHPクラスは素晴らしいでしょう。

皆さんありがとう

4

6 に答える 6

4

このクラスを確認する必要があります。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>";
于 2010-02-16T14:51:34.597 に答える
2

PHPのcurlライブラリを使用します。Webから他のページをプルして文字列としてフェッチし、正規表現を使用して文字列を解析して、ページのタイトルとメタタグを見つけることができます。

于 2010-02-16T14:40:02.840 に答える
2

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

両方のビットを組み合わせて、必要なものをすべて取得できるはずです。

于 2010-02-16T14:41:37.757 に答える
2

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");
}
于 2010-07-06T16:36:09.037 に答える
1

これを見てください。これは、ページメタタグを取得してさらに多くのことを行うためのジェネリッククラスです。これをcodeigniterライブラリに追加できるかどうかを確認してください。ありがとう

于 2010-02-16T14:41:53.997 に答える
0

これを試して:

    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;
于 2021-09-30T06:59:06.940 に答える