Extract HTML content (meta,title,img) From a Link with PHP

By Akbarpri    PHP

Sometimes we want to extract HTML content like Meta tags ( description,author,keywords etc), title, images from a website or a link. That's because we build a curator site, social media sharer etc. We can do it easily in PHP. 
Lets just code, create a file named Scrap.php   code it like this.
      public static function setUrl($url){                self::$url = $url;    }    
    //Check the url    public static function checkUrl(){        
        //valid url given...?        if (filter_var(self::$url, FILTER_VALIDATE_URL) === FALSE){                        return false;                   }else{            
            //cek the url status            $array = @get_headers(self::$url);            $status = $array[0];            if(strpos($status,"200") or strpos($status,"301")){                                return true;            }                       return false;        }                   }    
    //getting meta tags    public static function getMeta($attr=null){                if($attr){                    $meta = get_meta_tags(self::$url);            return isset($meta[$attr]) ? $meta[$attr] : 'No meta found';        }                return get_meta_tags(self::$url);    }        private static function explodeTitle($a,$b,$c){            $y = explode($b,$a);        $x = explode($c,$y[1]);        return $x[0];    }    
    //getting title     public static function getTitle(){                    return scrap::explodeTitle(file_get_contents(self::$url),"","");          }

    //get the Domain       public static function getDomain(){           $dmn = parse_url(self::$url);                    return $dmn['host'];    }   
    //getting images with DOMdocument    public static function getImg(){             $html = file_get_contents(self::$url);        $doc = new DOMDocument;                @$doc->loadHTML($html);        $tags = $doc ->getElementsByTagName('img');                $arr = array();                foreach ($tags as $tag) {                        $arr[] = $tag->getAttribute('src');        }        
        //return only the first image        if(!empty($arr))            return $arr[0];                return "http://codetrash/assets/images/sains.png";    }}
Now, we call the Class inside index.php. So create a file named index.php and code it
Title : ".$obj::getTitle()."
";    echo "Description : ".$obj::getMeta('description')."
";    echo "Author : ".$obj::getMeta('author')."
";    echo "Image : ".$obj::getImg()."
";    }else{    echo "Invalid Domain,";}
Run it yourself. the codes above will output smth like this
Title : Accessing Controller Layout Inside Route in Laravel 4Description : If you're using Dynamic Controller layout in Laravel, you might sometimes want to directly render the Views inside Route. This is actually not a recommended way to render views inside Route, but whatever, you're the master of your own program. bellow is how we do it in Controller :class HomeController extends BaseController { public function showHome() { now you can...Author : https://plus.google.com/103246562201441175208Image : http://codetrash.com/assets/images/logo.png

NOTES !. the codes may not be the best for you. have it your way :D.

BTW joellarson has written a much more better implementation of this here

Comments



    Follow Us