Showing posts with label link preview like facebook using php. Show all posts
Showing posts with label link preview like facebook using php. Show all posts

Wednesday 27 December 2017

link preview like facebook using php

link preview like facebook using php



Hello here example with text area 

<?php

$string =  "adasd asdad asdasd https://www.facebook.com asdasd asdasdsa ";

$preview_data = array();
$preview_data['title'] = "";
$preview_data['desc'] = "";
$preview_data['url'] = "";
$preview_data['image'] = "";

$myurl = "";
if($string!=''){
$string_array = explode(" ",$string);
if(count($string_array) > 0){
foreach($string_array as $string_value){
if (filter_var($string_value, FILTER_VALIDATE_URL)) {
$myurl = $string_value;
break;
} // if of url
if($string_value!=''){
$sub_string_check = explode(".",$string_value);
if(count($sub_string_check) >= 2){
$myurl = "http://" .$string_value;
break;
}
}
} // foreach for loop
 
} // if for array count
}// main if condition


if (filter_var($myurl, FILTER_VALIDATE_URL)) {
$agent = "'Mozilla/5.0 (Windows NT 6.2; WOW64; rv:17.0) Gecko/20100101 Firefox/17.0';";
$ch = curl_init();
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FAILONERROR, true);
    curl_setopt($ch, CURLOPT_URL, $myurl);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
curl_setopt($ch, CURLOPT_USERAGENT, $agent);
    
    $data = curl_exec($ch);
    curl_close($ch);
$dom = new DOMDocument();
@$dom->loadHTML($data);
//$page = new self();
    
    // Parse DOM to get Title
    $nodes = $dom->getElementsByTagName('title');
    $title = $nodes->item(0)->nodeValue;
    
    // Parse DOM to get Meta Description
    $metas = $dom->getElementsByTagName('meta');
    $body = "";
    for ($i = 0; $i < $metas->length; $i++) {
        $meta = $metas->item($i);
$name = $meta->getAttribute('property');
$name123 = $meta->getAttribute('name');
        //if ($meta->getAttribute('name') == 'description') {
             $body[$name] = $meta->getAttribute('content');
$body[$name123] = $meta->getAttribute('content');
        //}
    }
    
    // Parse DOM to get Images
    $image_urls = array();
$image_src = array();
    $images = $dom->getElementsByTagName('img');
     
     for ($i = 0; $i < $images->length; $i ++) {
         $image = $images->item($i);
         $src = $image->getAttribute('src');
         
         if(filter_var($src, FILTER_VALIDATE_URL)) {
             $image_src[] = $src;
         }
     }
    
    $output = array(
        'title' => $title,
        'image_src' => $image_src,
        'body' => $body
    );
echo "<pre>";
print_r($output);
$title = "";
$description ="";
$url = "";
$image_url = "";
if(isset($output['title'])){
$title = $output['title'];
}elseif(isset($output['body']['og:title'])){
$title = $output['body']['og:title'];
}elseif(isset($output['body']['twitter:title'])){
$title = $output['body']['twitter:title'];
}
if(isset($output['body']['description'])){
$description = $output['body']['description'];
}elseif(isset($output['body']['og:description'])){
$description = $output['body']['og:description'];
}elseif(isset($output['body']['twitter:description'])){
$description = $output['body']['twitter:description'];
}
if(isset($output['body']['url'])){
$url = $output['url'];
}elseif(isset($output['body']['og:url'])){
$url = $output['body']['og:url'];
}elseif(isset($output['body']['twitter:url'])){
$url = $output['body']['twitter:url'];
}elseif(isset($output['body']['al:web:url'])){
$url = $output['body']['al:web:url'];
}else{
$url =$myurl;
}
if(isset($output['body']['image'])){
$image_url = $output['image'];
}elseif(isset($output['body']['og:image'])){
$image_url = $output['body']['og:image'];
}elseif(isset($output['body']['twitter:image'])){
$image_url = $output['body']['twitter:image'];
}elseif(isset($output['image_src'][0])){
$image_url = $output['image_src'][0];
}
$preview_data['title'] = $title;
$preview_data['desc'] = $description;
$preview_data['url'] = $url;
if($image_url!=""){
$preview_data['image'] = filter_var($image_url, FILTER_VALIDATE_URL)?$image_url:$url.$image_url;
}
 
 
/*if(!filter_var($image_url, FILTER_VALIDATE_URL)) {
             
foreach($output['body'] as $t){
$preview_data['image'] = $url.$t;
}
 
         }*/
}
echo "<pre>";
print_r($preview_data);

?>