A programming & designing blog!

Monday, November 4, 2013

Fetch Facebook OG Meta Tags with PHP

OG (Open Graph) meta tag is designed by Facebook to make url share easy on social networking websites. Now days these tags are widely used by other websites. If you are running a social networking website or planning to run one. You can make url sharing easy by fetching these tags.

Today I will show you how we can get/fetch OG meta tags with PHP script.


fetch_og PHP function

/**
 * Fetch OG Metatags
 * @param string $url
 * 
 * @return array
 */
function fetch_og($url)
{
    $data = file_get_contents($url);
    $dom = new DomDocument;
    @$dom->loadHTML($data);
     
    $xpath = new DOMXPath($dom);
    # query metatags with og prefix
    $metas = $xpath->query('//*/meta[starts-with(@property, \'og:\')]');

    $og = array();

    foreach($metas as $meta){
        # get property name without og: prefix
        $property = str_replace('og:', '', $meta->getAttribute('property'));
        # get content
        $content = $meta->getAttribute('content');
        $og[$property] = $content;
    }

    return $og;
}

Usages

$og = fetch_og('http://www.w3bees.com');
echo $og['title'];

35 comments:

  1. Replies


    1. Hai ma thú cấp tám bên trong đều là ma thú cấp tám sơ kỳ.

      Hắc Ám Thần Điện lần này xuất ra tổng cộng là bảy con ma thú hắc ám, bây giờ chỉ còn lại ba con, nhưng ba con chỉ là ma thú cấp tám sơ kỳ.

      Cự Viên Tộc ma thú đối với ma thú của Hắc Ám Thần Điện phát khởi công kích, Nhạc Thuần và Nhạc Sử Chân cũng đều chọn một hán tử áo đen mà tấn công.

      Loại thực lực đối kháng này hoàn toàn kém xa, Nhạc Sử Chân và Nhạc Thuần hai người cũng chỉ có tu vi thất tinh Đấu Tôn, cộng them với đấu kỹ cao thâm của Nhạc gia, khiến cho hán tử áo đen bị trọng thương.

      Kỳ thật bốn hán tử áo đen này lúc đầu đã muốn chạy trốn nhưng không thể nào thoát ra hkoir.

      Bọn họ khôdongtam
      mu moi ra hom nay
      tim phong tro
      http://nhatroso.com/
      nhạc sàn
      tổng đài tư vấn luật
      văn phòng luật hà nội
      tổng đài tư vấn luật
      thành lập công ty trọn gói
      http://we-cooking.com/
      chém gió
      trung tâm tiếng anhng có cơ hội trốn mà thôi, nếu như thật sự chạy thoát thì cũng bị nhốt trong cấm chế của Nhạc Thành.

      Lúc này, Tiểu Song ở phía xa xa không làm gì, hai người tự động lùi ra ngoài, miễn cho mọi người bị vưỡng chân vướng tay.

      Hỏa diễm công kích của Địa Ma Lão Nhân không lâu đã trở nên tán loạn, nửa năm

      Delete
  2. For some URLs I never get the correct response.

    Can anyone please help me on this issue.

    ReplyDelete
  3. for any post from nytimes.com I always get null while fetching og.

    Please help !

    ReplyDelete
  4. This blog is really informative. I gained more knowledge by reading your post. Thanks for providing us the useful information with us. Your blog is really helpful for me in attaining my knowledge in a right way.

    CCNA Training in Chennai

    ReplyDelete
  5. Thank you for having taken your time to provide us with your valuable information relating to your stay with us.we are sincerely concerned.., Most importantly, you Keepit the major...
    SEO Training in Chennai

    ReplyDelete
  6. Great Article I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because it becomes more and more interesting from the starting lines until the end. So Thank you for sharing a COOL Meaningful stuff with us Keep it up..!

    SAP training in Chennai

    ReplyDelete
  7. what if there are multiple og:images??

    Its gets me the last one. I want to get the first og:image instead

    ReplyDelete
  8. Thanks for this wonderful post. The information in this article is very helpful to me. Thanks a lot for sharing. Keep blogging.
    Best Truth or Dare Questions for Kids

    ReplyDelete
  9. Thanks for this wonderful post. The information in this article is very helpful to me. Thanks a lot for sharing. Keep blogging.
    Interest Waypoint

    ReplyDelete
  10. You truly did more than visitors’ expectations. Thank you for rendering these helpful, trusted, edifying and also cool thoughts on the topic.
    ME/M.Tech Project Center in Chennai | ME/M.Tech Project Center in Velachery

    ReplyDelete
  11. Thanks for sharing. I see the novelty of your writing, I will share it for everyone to read together. I look forward to reading many articles from you geometry dash

    ReplyDelete
  12. Needed to compose one little word yet thanks for the suggestions that you are contributed here...
    Best Online Software Training Institute | RPA Training

    ReplyDelete
  13. Very useful article. I have understood many applications of hot mail
    hotmail.com sign in

    ReplyDelete
  14. cara menggugurkan hamil kandungan dengan cepat dan akurat terbukti ampuh untuk melunturkan janin kehamilan muda 1 minggu hingga 1 , 2 , 3 dan 4 bulan
    obat penggugur kandungan adalah suatu bentuk proses berakhirnya kehamilan dengan dikeluarkannya janin ( fetus ) atau embrio sebelum memiliki kemampuan untuk berkembang dan bertahan hidup diluar rahim , sehingga bisa menyebabkan kematiannya pada janin
    Disini akan kami beritahukan kepada Anda langkah cara cepat menggugurkan kandungan dengan cepat dan selamat adalah KURET dan obat aborsi
    Kunyit memang tidak di baik di konsumsi oleh ibu hamil yang sedang hamil muda sehubungan dengan sifatnya untuk melancarkan haid , dan di percayai sebagai obat telat datang bulan
    jual obat aborsi paling manjur Misiprostol Cytotec asli terbukti ampuh melunturkan janin kuat tanpa kuret dengan bersih untuk usia 1 , 2 , 3 sampai 4 bulan dengan aman

    ReplyDelete
  15. Thanks for the command of this topic, thanks to that I got here. Cool thing.

    Tamara

    ReplyDelete
  16. Really great work. Your article was very helpful.Thanks for sharing valuable points.Keep sharing.Thank You
    rpa training in chennai | rpa training in velachery | trending technologies list 2018

    ReplyDelete
  17. Thanks for such a nice article on Blueprism.Amazing information of Blueprism you have . Keep sharing and updating this wonderful blog on Blueprism
    Thanks and regards,
    blue prism training in chennai
    blue prism training institute in chennai
    Blueprism certification in chennai

    ReplyDelete
  18. Supreet Solutions is the Best RPA Online Training Institute in Hyderabad which offers RPA Online Training by 15+ years of industry experts. From our training, you can acquire working knowledge skills on various Robotics Tools like Automation Anywhere, Blue Prism, UI Path. We Provide Best RPA Online Training With Live Practicals.
    Best, Live RPA Online Training In USA,Hyderabad | Online RPA Training In USA, Hyderabad
    Click Here Best RPA Online Training to go to RPA Online Training Institute Details.

    ReplyDelete
  19. Best RPA Online Training Institute In Hyderabad, RPA Online Training Institute In Hyderabad, Best RPA Online Training In Hyderabad, RPA Course In Hyderabad, RPA Training, RPA Training In Hyderabad, RPA Online Training In Hyderabad, Blue Prism Training In Hyderabad, Automation Training In Hyderabad, Blue Prism Online Training In Hyderabad, Automation Online Training In Hyderabad, Automation Course In Hyderabad, Automation Online Course In Hyderabad, Automation Training Institutes In Hyderabad, Automation Online Training Institutes In Hyderabad, RPA Course, Automation Course, Automation Training, RPA Course Fee In Hyderabad, RPA Training In Hyderabad Ameerpet, Best RPA Training Institutes In Hyderabad, RPA Training In Ameerpet, RPA Online Training Institutes In Hyderabad.
    Best, Live RPA Online Training In USA,Hyderabad | Online RPA Training In USA, Hyderabad
    Click Here Best RPA Online Training to go to RPA Online Training Institute Details.

    ReplyDelete
  20. Needed to compose one little word yet thanks for the suggestions that you are contributed here...

    Python Training in Chennai

    ReplyDelete
  21. Fiz uma next to have access to, you can access your email, you can review the email using the system, then click here to confirm the last 4 to do it In the last part of you in the last part of it 83 brain da certo oque eu faço.
    delete hotmail account

    ReplyDelete
  22. RPA Training in hyderabad is offered by Digital Nest, Which Specialises in training RPA courses on RPA tools Like Blue Prism Training, ui path training, Automation anywhere .

    RPA training is offered by real time industry experts in hyderabad by Experts from Tech Mahindra, Infosys, Delioitte. who has 10+ years of experience in dealing with various Tools and techniques specially in automations.

    RPA training in hyderabad is in demand as many students and working professionals from various background are looking forward to the best courses that can help them in switching their jobs and also help the freshers grabbing jobs

    ReplyDelete
    Replies
    1. https://www.digitalnest.in/rpa-training-in-hyderabad/

      Delete

  23. Greetings. I know this is somewhat off-topic, but I was wondering if you knew where I could get a captcha plugin for my comment form? I’m using the same blog platform like yours, and I’m having difficulty finding one? Thanks a lot.

    Advanced AWS Online Training | Advanced Online AWS Certification Course - Gangboard
    Best AWS Training in Chennai | Amazon Web Services Training Institute in Chennai Velachery, Tambaram, OMR
    Advanced AWS Training in Bangalore |Best AWS Training Institute in Bangalore BTMLA ,Marathahalli

    ReplyDelete
  24. Thank you for sharing such great information with us. I really appreciate everything that you’ve done here and am glad to know that you really care about the world that we live in
    python training Course in chennai
    python training in Bangalore
    Python training institute in bangalore

    ReplyDelete