Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function getLinkPreview($url){
- if($url == null || $url == ''){
- return [];
- }
- try {
- $doc = new DOMDocument();
- $data = file_get_contents($url);
- @$doc->loadHTML($data);
- $rv = array(
- 'title' => '',
- 'url' => $url,
- 'description' => '',
- 'image' => '',
- );
- // meta name="title"
- // meta name="og:title"
- // title
- $_title = $doc->getElementsByTagName('title');
- foreach($_title as $_ttl){
- $rv['title'] = $_ttl->textContent;
- }
- // meta name="description"
- // meta name="og:description"
- // meta name="image"
- // meta name="og:image"
- $_meta = $doc->getElementsByTagName('meta');
- if($rv['description'] == ''){
- foreach($_meta as $_des){
- $_mName = $_des->getAttribute('name');
- if($_mName == 'description'){
- $rv['description'] = $_des->getAttribute('content');
- }
- }
- if($rv['description'] == ''){
- foreach($_meta as $_des){
- $_mName = $_des->getAttribute('property');
- if($_mName == 'description'){
- $rv['description'] = $_des->getAttribute('content');
- }
- }
- }
- if($rv['description'] == ''){
- foreach($_meta as $_des){
- $_mName = $_des->getAttribute('name');
- if($_mName == 'og:description'){
- $rv['description'] = $_des->getAttribute('content');
- }
- }
- }
- if($rv['description'] == ''){
- foreach($_meta as $_des){
- $_mName = $_des->getAttribute('property');
- if($_mName == 'og:description'){
- $rv['description'] = $_des->getAttribute('content');
- }
- }
- }
- }
- if($rv['image'] == ''){
- foreach($_meta as $_des){
- $_mName = $_des->getAttribute('name');
- if($_mName == 'image'){
- $rv['image'] = $_des->getAttribute('content');
- }
- }
- if($rv['image'] == ''){
- foreach($_meta as $_des){
- $_mName = $_des->getAttribute('property');
- if($_mName == 'image'){
- $rv['image'] = $_des->getAttribute('content');
- }
- }
- }
- if($rv['image'] == ''){
- foreach($_meta as $_des){
- $_mName = $_des->getAttribute('name');
- if($_mName == 'og:image'){
- $rv['image'] = $_des->getAttribute('content');
- }
- }
- }
- if($rv['image'] == ''){
- foreach($_meta as $_des){
- $_mName = $_des->getAttribute('property');
- if($_mName == 'og:image'){
- $rv['image'] = $_des->getAttribute('content');
- }
- }
- }
- }
- if($rv['image'] == ''){
- $_imgs = $doc->getElementsByTagName('img');
- if(count($_imgs) > 0){
- $rv['image'] = $_imgs[0]->getAttribute('src');
- }
- }
- $rv['title'] = str_replace("'","’",$rv['title']);
- $rv['description'] = str_replace("'","’",$rv['description']);
- return $rv;
- } catch (Exception $e){
- $rv = array(
- 'title' => '',
- 'url' => $url,
- 'description' => '',
- 'image' => '',
- );
- return $rv;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement