Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

get meta description , title and image from url like facebook link sharing

my code is

       function getTitle($Url){
            $str = file_get_contents($Url);
            if(strlen($str)>0){
                preg_match("/\<title\>(.*)\<\/title\>/",$str,$title);
                return $title[1];
            }
            else
            {
                return false;
            }
        }
        function getMetas($Url){
            $str = file_get_contents($Url);
            if(strlen($str)>0){
             //   preg_match("/\<title\>(.*)\<\/title\>/",$str,$title);
               preg_match("/<meta name=\"description\" content=\"(.*?)\"/",$str,$title);
              //  preg_match( '<meta name="description".*content="([^"]+)">siU', $str, $title);
                return $title[1];
            }
             else
            {
                return false;
            }
        }

        //Example:
        $url=$_POST['url'];
        echo getTitle($url);
        echo "<br><br>";
        echo getMetas($url);

this does not shows result for all the url's , example http://google.com

like image 968
Steve Bals Avatar asked Apr 19 '14 04:04

Steve Bals


People also ask

How do you get the URL of a picture on Facebook?

Right-click the image and click the option to copy its URL. Depending on the browser you use, the option reads "Copy Image Location," "Copy Image Address," "Copy Link" or "Copy Image URL." In Internet Explorer, click the "Properties" option and copy the URL from the Properties window.

How do I share a picture from a website to Facebook?

go to your Facebook timeline. paste the url into the What's on your mind textbox. upload image.

How do I choose a Facebook link preview image?

Enter the link to your site, click the 'scrape' button a few times. Keep clicking it until your new preview image you just uploaded appears in the preview area. And presto!


3 Answers

Why are you using regular expression for parsing the <meta> tags ?

PHP has an in-built function for parsing the meta information , it is called the get_meta_tags()

Illustration :

<?php
$tags = get_meta_tags('http://www.stackoverflow.com/');
echo "<pre>";
print_r($tags);

OUTPUT:

Array
(
    [twitter:card] => summary
    [twitter:domain] => stackoverflow.com
    [og:type] => website
    [og:image] => http://cdn.sstatic.net/stackoverflow/img/[email protected]?v=fde65a5a78c6
    [og:title] => Stack Overflow
    [og:description] => Q&A for professional and enthusiast programmers
    [og:url] => http://stackoverflow.com/
)

As you can see the title , image and description are being parsed which you really want.

like image 105
Shankar Narayana Damodaran Avatar answered Oct 30 '22 16:10

Shankar Narayana Damodaran


I know the question is 1.5 years old. But if you are still looking for it, you can use https://urlmeta.org. Its a free API to extract URL meta.

like image 45
Moin Avatar answered Oct 30 '22 18:10

Moin


You can check a URL for http or https by

$url='stackoverflow.com';
$http_check='http://';
$https_check='https://';
if(substr($url,0,7)!=$http_check){
   $url=$http_check.$url;
}else if(substr($url,0,8)!=$https_check){
   $url=$https_check.$url;
}else{
    $url=$url
}

then you can use the above answer

<?php
$tags = get_meta_tags($url);
echo "<pre>";
print_r($tags);
like image 21
Faizan Amin Avatar answered Oct 30 '22 17:10

Faizan Amin