-8

How to share a link with image and description on custom website in PHP like linkedin, facebook.

If users share any valid link it show image with descriptions like facebook and linkedin.

Sanjay Shah
  • 2,809
  • 2
  • 19
  • 20

3 Answers3

3

You want to read about Facebook Open Graph meta tags. They allow you to edit the way shared post is presented. More info on that can be found in this article http://davidwalsh.name/facebook-meta-tags

Kasia Gogolek
  • 3,374
  • 4
  • 33
  • 50
1

You need to get the content of given URL, maybe using file_get_contents or some similar method (e.g: this) and parse the content of this URL.

Let's try on very basic example;

$url = get_url_from_user_post($user_post);
// get page contents (as html)
$page_contents = file_get_contents($url);

// search for title
// option 1
preg_match('~<meta\s+property="og:title"\s+content="(.*?)".*?>~um', $page_contents, $matches);
// option 2
preg_match("~<title>(.*)</title>~um", $page_contents, $matches);

$page_title = trim($matches[1]);

// search for an image
// option 1
preg_match('~<meta\s+property="og:image"\s+content="(.*?)".*?>~um', $page_contents, $matches);
// option 2
preg_match_all('~<img.*?src="(.*?)".*?>~um', $page_contents, $matches);
if (!empty($matches)) {
    // ... and get one of them

And you have more options to do this, getting/parsing html content of page. See: http://php.net/manual/en/book.dom.php, or getting meta tags with PHP (get_meta_tags).

Community
  • 1
  • 1
Kerem
  • 11,377
  • 5
  • 59
  • 58
1

Okay, what I am getting is you want to get an image and basic description of the site a user links to.

To get the description, the most simplest method would be to use the get_meta_tags function in php, and use the description.

$meta = get_meta_tags($some_url); 
//description will be stored in $meta['description'] if it was found from $some_url.

This relies on the site to provide proper meta tags, which won't always work out in your favour, so you will need a backup method (such as document title or checking html for words) or a default description value incase you don't get anything at all.

For getting the snapshot of a website, you could rely on a third party service such as picoshot.com or websnapr.com/, but in quite a few cases that may not be the best option to go with. If you wanted to do the snapshot on your own, you could do so but the process would be complex, you will need a good amount of control over your server and environment, you could check out something like this to get you started with snapshots. If you have an environment such as Linux, that script won't work as it uses an internet explorer browser capture, you can check here for some Linux workarounds.