Facebook Like Preview Box in PHP | URL Preview Box

By | July 16, 2019

Facebook like preview box is very useful to get the information about the link we want to share. Recently i was working over a project and i was in need of this preview box. While thinking about this i remember i have created a script regarding the Facebook like preview box in PHP 5 or 6 Years back. I used that code made some changes and created a class of it. Now you can use it over your project you can download it from the bottom of the article. So let’s start that how i have created the class what functions are working over it and how we will get the URL Preview Box Result.

In this class i have used the image scroll. I use to try to get the whole images of the page then you can choose whatever suits you.

If you are working over the Localhost then “https” links will not work. They will work when you put the code over the server.




Things we collect in this class are

  • Title
  • Description
  • Keywords
  • Image
  • OgImage

Index.php

We use this page to create a form for taking the URL from the user. And a div to show the url elements. For processing the URL we use the Ajax to get the values.

Above form will show the input box and a button over the page. Now we will put the Jquery which uses the click function for showing the result. Below function will show the loader icon then get the URL from the input box. Send the request to shareDetails.php for processing the url. We will send the URL as parameter for process. If our code returns the success and worked perfectly then it will hide the loading icon and parse the received Json and show the values in the Title box which is a part of bigbox.

Very small css to show everything perfectly.

As you can see we have setup the HTML page and everything is looking prefect. Now we will move to our main class functions which has created the magic behind the walls. We have created some functions so that we will get the perfect details.

shareDetails.php

This is our class which is having all the functions which will do the magic.




initializeDom()

As the function name suggest that it will initialize the Dom and it takes URL as parameter. This url is the url which we will use to put in the input box. In this function we will create a CURL request and create an object of DOM. We will use the loadhtml function for getting the pagedetails. Our function will return the dom which is having whole details of the page.

getWebsiteTitle()

This function will take out the title of the website.

getWebsiteDescription()

This function will take the description the website. Which is available at the meta tag.

getWebsiteKeyword()

Keyword function will take out the keywords of the website.

getWebsiteImages()

It will take the whole images of the website. You can scroll the website images and you can use whichever you want.

getWebsiteOgImage()

Some of the websites dont use images but they use OgImage tag for showing the image at the time of sharing. So this function will take that URL.

Hope you like this tutorial if you have any issues please comment below. You can also download the files of this class.



Facebook Like URL Preview Box
Facebook Like URL Preview Box
urlPreview.zip
Version: 1.0
177.9 KiB
11 Downloads
Details...

Leave a Reply

Your email address will not be published. Required fields are marked *