<?php 
 ini_set('allow_url_fopen', '1');
class metatags {

    var $title = '';

    var $description = '';

    var $error_invalid_url = 'Error';   //message to display if the url can't be loaded

    var $error_no_title = 'Untitled';   //display if title is not set

    var $error_no_desc = 'None set';    //display if description is not set

     

    function getmetadata($url){
    	
    	
	    //If Url is empty or contain just white-spaces
		if(trim($url) == '')
		{
			echo $this->error_invalid_url;
			exit();
		}
		//Exclude White-Spaces
		else {$url = trim($url);}

    	
        //make sure URL is formated correctly

        if (strstr($url, 'http://') == false && trim($url) != '' ){

            $url = 'http://'.$url;
        }

        //get file contents
		
		$context = stream_context_create(array(
    'http' => array(
        'method' => 'POST',
        'header' => implode("\r\n", array(
                'Content-type: application/x-www-form-urlencoded',
                'Accept-Language: en-us,en;q=0.5', // optional
                'Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7' // optional
        )),
        'content' => http_build_query(array(
                'prev'  =>      '_t',
                'hl'    =>      'en',
                'ie'    =>      'UTF-8',
                'text'  =>      'hello',
                'sl'    =>      'en',
                'tl'    =>      'ru'
        ))
    )
));


   $d= file_get_contents($url,false,$context );
		
 

        //display error if site can't be loaded

        if (!$d) {

            echo $this->error_invalid_url;

            exit();

        }

        //shorten string

        $line = substr($d, 0, 30000);

            //remove linebreaks from the string

            $linebreaks   = array("\r\n", "\n", "\r");

            //Processes \r\n's first so they aren't converted twice.

            $line = str_replace($linebreaks, '', $line);

            // This only works if the title and its tags are on one line

            if (preg_match("/<title>(.*)<\/title>/", $line, $out)) {

                $this->title = $out[1];

            }

        //get description

        $desc = get_meta_tags($url);

        $this->description = $desc['description'];  

         

    }

    //get title

    function get_title(){

        if(!$this->title){

            return $this->error_no_title;

        }else{

            return $this->title;

        }  

    }

    //get description

    function get_description(){

        if(!$this->description){

            return $this->error_no_desc;

        }else{

            return $this->description;  

        }

    }

}
?>
