【问题标题】:CodeIgniter: A Class/Library to help get meta tags from a web page?CodeIgniter:帮助从网页获取元标记的类/库?
【发布时间】:2011-01-17 10:18:58
【问题描述】:

我正在使用 codeigniter。我想我使用哪个 php 框架并不重要。

但在我编写自己的课程之前,是否已经编写了另一个课程,允许用户获取任何站点的页面标题和元标记(关键字、描述)……如果有的话。

任何类型的 PHP 类都可以做到这一点。

谢谢大家

【问题讨论】:

    标签: php codeigniter html-parsing meta-tags


    【解决方案1】:

    使用 PHP 的 curl 库。它可以从 web 中拉取其他页面并将它们作为字符串获取,然后您可以使用正则表达式解析字符串以找到页面的标题和元标记。

    【讨论】:

      【解决方案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

      您应该能够将两者的位组合起来以获得所需的一切。

      【讨论】:

        【解决方案3】:

        See this please. 这是获取页面元标记并做更多事情的通用类。看看你是否可以在 codeigniter 库中添加它。谢谢

        【讨论】:

          【解决方案4】:

          你应该看看这个类:PHP Simple HTML DOM 它是这样工作的:

          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>";
          

          【讨论】:

          【解决方案5】:

          使用 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");
          }
          

          【讨论】:

            【解决方案6】:

            试试这个:

                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;
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2019-10-24
              • 2013-05-01
              • 1970-01-01
              • 1970-01-01
              • 2018-05-24
              • 2021-06-05
              相关资源
              最近更新 更多