2017-12-17 4 views
1

を期待通りに動作しない(特別OG:タイトル、OG:説明とOG:画像を)...PHPのget_meta_tags()私はメタタグ取得したい

私は次のコードを使用し、

$tags = get_meta_tags('https://www.shoutmeloud.com/review-of-hostgator-webhosting-wordpress.html/'); 
echo "<pre>"; 
print_r($tags); 
それはタグを返さなかったのはなぜ http://phpfiddle.org/main/code/qe22-3r5n

それはPHPFidlle次の配列、

Array 
(
    [viewport] => width=device-width, initial-scale=1 
    [description] => Check out HostGator review for year 2017. This review if written after using Hostgator for 5 years. Is Hostgator Good hosting? Find out answer here 
    [twitter:card] => summary_large_image 
    [twitter:description] => Check out HostGator review for year 2017. This review if written after using Hostgator for 5 years. Is Hostgator Good hosting? Find out answer here 
    [twitter:title] => A Blogger Review of HostGator Shared Hosting Plan 
    [twitter:site] => @shoutmeloud 
    [twitter:image] => https://www.shoutmeloud.com/wp-content/uploads/2014/08/Review-of-Hostgator-Shared-Hosting-Plan.jpg 
    [twitter:creator] => @denharsh 
    [generator] => Easy Digital Downloads v2.8.14 
    [msapplication-tileimage] => https://www.shoutmeloud.com/wp-content/uploads/2017/12/cropped-favicon-270x270.png 
) 

を返します。 og:title, og:description and og:imageのようにそれらのURLのソースコードにもあります。この問題を解決するにはどうすればよいですか?

+0

はこの回答をお試しください:https://stackoverflow.com/a/12014561/8566549 –

答えて

0

get_meta_tagsname属性を探しているからです。

プロパティの値は、あなたがproperty属性の下に表示されている探しているタグのキー

すべてになります。

ソースページのオーナーであれば、属性と同じ値を持つ要素にname属性を追加するだけで済みます。

もしそうでなければ、のようなものをfile_get_contentsまたはcurlと組み合わせて、興味のある特定の値を得ることができます。

例えば:中

<?php 
$content = file_get_contents('https://www.shoutmeloud.com/review-of-hostgator-webhosting-wordpress.html'); 

$doc = new DOMDocument(); 

// squelch HTML5 errors 
@$doc->loadHTML($content); 

$meta = $doc->getElementsByTagName('meta'); 
foreach ($meta as $element) { 
    $tag = []; 
    foreach ($element->attributes as $node) { 
     $tag[$node->name] = $node->value; 
    } 
    $tags []= $tag; 
} 

print_r($tags); 

結果:

Array 
(
    [0] => Array 
     (
      [charset] => UTF-8 
     ) 

    [1] => Array 
     (
      [name] => viewport 
      [content] => width=device-width, initial-scale=1 
     ) 

    [2] => Array 
     (
      [name] => description 
      [content] => Check out HostGator review for year 2017. This review if written after using Hostgator for 5 years. Is Hostgator Good hosting? Find out answer here 
     ) 

    [3] => Array 
     (
      [property] => og:locale 
      [content] => en_US 
     ) 

    [4] => Array 
     (
      [property] => og:type 
      [content] => article 
     ) 

    [5] => Array 
     (
      [property] => og:title 
      [content] => A Blogger Review of HostGator Shared Hosting Plan 
     ) 

    [6] => Array 
     (
      [property] => og:description 
      [content] => Check out HostGator review for year 2017. This review if written after using Hostgator for 5 years. Is Hostgator Good hosting? Find out answer here 
     ) 

    [7] => Array 
     (
      [property] => og:url 
      [content] => https://www.shoutmeloud.com/review-of-hostgator-webhosting-wordpress.html 
     ) 

    [8] => Array 
     (
      [property] => og:site_name 
      [content] => ShoutMeLoud 
     ) 

    [9] => Array 
     (
      [property] => article:publisher 
      [content] => https://www.facebook.com/shoutmeloud 
     ) 

    [10] => Array 
     (
      [property] => article:author 
      [content] => https://www.facebook.com/denharsh 
     ) 

    [11] => Array 
     (
      [property] => article:tag 
      [content] => hostgator 
     ) 

    [12] => Array 
     (
      [property] => article:tag 
      [content] => Review 
     ) 

    [13] => Array 
     (
      [property] => article:tag 
      [content] => Wordpress Webhosting 
     ) 

    [14] => Array 
     (
      [property] => article:section 
      [content] => Webhosting 
     ) 

    [15] => Array 
     (
      [property] => article:published_time 
      [content] => 2016-08-18T15:51:46+05:30 
     ) 

    [16] => Array 
     (
      [property] => article:modified_time 
      [content] => 2017-11-29T23:50:31+05:30 
     ) 

    [17] => Array 
     (
      [property] => og:updated_time 
      [content] => 2017-11-29T23:50:31+05:30 
     ) 

    [18] => Array 
     (
      [property] => fb:admins 
      [content] => 100000563323286 
     ) 

    [19] => Array 
     (
      [property] => og:image 
      [content] => https://www.shoutmeloud.com/wp-content/uploads/2014/08/Review-of-Hostgator-Shared-Hosting-Plan.jpg 
     ) 

    [20] => Array 
     (
      [property] => og:image:secure_url 
      [content] => https://www.shoutmeloud.com/wp-content/uploads/2014/08/Review-of-Hostgator-Shared-Hosting-Plan.jpg 
     ) 

    [21] => Array 
     (
      [name] => twitter:card 
      [content] => summary_large_image 
     ) 

    [22] => Array 
     (
      [name] => twitter:description 
      [content] => Check out HostGator review for year 2017. This review if written after using Hostgator for 5 years. Is Hostgator Good hosting? Find out answer here 
     ) 

    [23] => Array 
     (
      [name] => twitter:title 
      [content] => A Blogger Review of HostGator Shared Hosting Plan 
     ) 

    [24] => Array 
     (
      [name] => twitter:site 
      [content] => @shoutmeloud 
     ) 

    [25] => Array 
     (
      [name] => twitter:image 
      [content] => https://www.shoutmeloud.com/wp-content/uploads/2014/08/Review-of-Hostgator-Shared-Hosting-Plan.jpg 
     ) 

    [26] => Array 
     (
      [name] => twitter:creator 
      [content] => @denharsh 
     ) 

    [27] => Array 
     (
      [name] => generator 
      [content] => WordPress 4.9.1 
     ) 

    [28] => Array 
     (
      [name] => generator 
      [content] => Easy Digital Downloads v2.8.14 
     ) 

    [29] => Array 
     (
      [property] => fb:pages 
      [content] => 94019601674 
     ) 

    [30] => Array 
     (
      [name] => msapplication-TileImage 
      [content] => https://www.shoutmeloud.com/wp-content/uploads/2017/12/cropped-favicon-270x270.png 
     ) 

    [31] => Array 
     (
      [content] => Hostgator 
      [itemprop] => name 
     ) 

    [32] => Array 
     (
      [content] => 1 
      [itemprop] => worstRating 
     ) 

    [33] => Array 
     (
      [content] => 4.5 
      [itemprop] => ratingValue 
     ) 

    [34] => Array 
     (
      [content] => 5 
      [itemprop] => bestRating 
     ) 

    [35] => Array 
     (
      [content] => Hostgator 
      [itemprop] => name 
     ) 

) 
関連する問題