« Oatmeal

Tagged "code"

Follow this tag

2 sort of silly, yet useful PHP functions to get some basic info about a webpage:

<?php
// Used to get a target URL's title
function get_target_title($target_url) {
    if (!function_exists('curl_init')) {
        die('CURL is not installed!');
    }
    $ch = curl_init();
    curl_setopt($ch, CURLOPT_URL, $target_url);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.94 Safari/537.36');
    $html = curl_exec($ch);
    curl_close($ch);
    $dom  = new DOMDocument;
    @$dom->loadHTML($html);
    $title = $dom->getElementsByTagName('title')->item('0')->nodeValue;
    return $title;
}
?>

Similar, but slightly different…

<?php
// Used to get a target URL's metadata, open graph, twitter, etc.
function get_target_metadata($target_url) {
    $url = $target_url;
    $tags = array();
    $tags['url'] = $url;
    $meta_tags = get_meta_tags($url);
    $tags['meta'] = $meta_tags;
    $site_html=  file_get_contents($url);
    $og_matches=null;
    preg_match_all('~<\s*meta\s+property="(og:[^"]+)"\s+content="([^"]*)~i', $site_html,$og_matches);
    $og_tags=array();
    for($i=0;$i<count($og_matches[1]);$i++) {
        $og_tags[$og_matches[1][$i]]=$og_matches[2][$i];
    }
    $tags['open graph'] = $og_tags;
    return $tags;
}
?>