Skip to footer navigation.

« Oatmeal

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

// 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);
    $dom  = new DOMDocument;
    $title = $dom->getElementsByTagName('title')->item('0')->nodeValue;
    return $title;

Similar, but slightly different…

// 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);
    preg_match_all('~<\s*meta\s+property="(og:[^"]+)"\s+content="([^"]*)~i', $site_html,$og_matches);
    for($i=0;$i<count($og_matches[1]);$i++) {
    $tags['open graph'] = $og_tags;
    return $tags;