Skip to main content

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'];

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 your great and helpful presentation I like your good service. I always appreciate your post. That is very interesting I love reading and I am always searching for informative information like this.Also Checkout: blockchain technology training chennai | blockchain training institute in chennai | blockchain training in chennai | cryptocurrency training in chennai

    ReplyDelete

  12. Thank you so much for posting this. I really appreciate your work. Keep it up. Great work!Best Placement company in Hyderabad

    ReplyDelete
  13. Nice 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, they are becomes a more and more interesting from the starting lines until the end.

    Check out : big data training in velachery
    big data hadoop training cost in chennai
    big data training in chennai omr
    big data training in chennai velachery

    ReplyDelete
  14. It’s interesting content and Great work. Definitely, it will be helpful for others. I would like to follow your blog. Keep post

    Check out:
    best hadoop training in omr
    hadoop training in sholinganallur
    best institute for big data in chennai
    big data hadoop course in chennai with placement

    ReplyDelete

Post a Comment

Popular posts from this blog

Multiple File Upload with Progress Bar using PHP & jQuery

Some days age I have created a post that deals with How we can upload file with progress bar using php and jQuery. Some of my readers asked me how we can upload multiple files with progress bar. In this post I am going to show you how we can upload multiple files with progress bar.We can upload multiple files with progress bar exactly same way as I shown previous. But we need to do some modification with our file upload form and our php script to handle multiple files.

Force File Download with PHP

If you want to control or track your downloadable contents then a force file downloader script can give you more option. Especially if you want make images or other stemming contents like music file (mp3, wav, ogg), video files (flv, mp4, ogg, 3gpp), css, JavaScripts or even a php file downloadable then you must need to use a force file downloader script.
In this post I am going to share a simple but useful force file downloader script.

File Upload with Progress Bar using jQuery and PHP

In this post you will learn how to create a AJAX file uploading system with progress bar using jQuery and PHP. For this purpose we will going to use jQuery Form Plugin. It is a easy to use and powerful jQuery AJAX form submitting plugin. It supports XMLHttpRequest Level 2 and iframe file transportation. That's why it will work for both old and new browsers.