Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
Node is saved as draft in My Content >> Draft
  • How to get Open Graph Protocol of a webpage by php?

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 2
    Comment on it
    // Set url
    $url = 'http://edition.cnn.com/2015/04/03/politics/white-house-congress-iran-nuclear-deal/index.html';
    
    // This function is going to go to a url or file and pull all the data that's held in it.
    $html = file_get_contents($url);
    
    // This function allows user to disable standard libxml errors and enable user error handling if you are so worried about using @ with warnings
    libxml_use_internal_errors(true);
    
    // Load the document
    $doc = new DomDocument();
    
    // Set array to hold meta name & value
    $data = array();
    
    if (!empty($html)) {
        if ($doc->loadHTML($html)) {
            $xpath = new DOMXPath($doc);
            $query = '//*/meta[starts-with(@property, \'og:\')]';
            $metas = $xpath->query($query);
    
            foreach ($metas as $meta) {
    
                // Get name
                $property = $meta->getAttribute('property');
    
                // Get value
                $content = $meta->getAttribute('content');
    
                // Assign name & value to array
                $data[$property] = $content;
            }
        }
        else {
            foreach (libxml_get_errors() as $error) {
                // handle errors here
            }
        }
    }
    print_r($data);
    
    get og data in php

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: