Last active
February 3, 2022 11:52
-
-
Save codeeshop-oc/2f0048d74c9ce7232d218ee96b09b69f to your computer and use it in GitHub Desktop.
Generating Previews with PHP
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public function link_preview_post() | |
{ | |
$post = json_decode($this->security->xss_clean($this->input->raw_input_stream)); | |
$url = $post->url; | |
$is_facebook_url = false; | |
$no_data = false; | |
$array = [ | |
'status' => false, | |
'tags' => '', | |
]; | |
if($url) { | |
// As previews don't change that often it would be good to save them for quick fetch results than scraping it all over again | |
$this->load->model('PreviewModel'); | |
$result = $this->PreviewModel->getByURL($url); | |
if($result) { | |
$array = $result; | |
} else { | |
$res = $this->curl_get($url, []); | |
$html = $res[1]; | |
$newurl = $res[0]; | |
$rmetas = []; | |
// Fetching meta tags from the URL given | |
$doc = new DomDocument(); | |
@$doc->loadHTML(html_entity_decode(mb_convert_encoding($html, 'HTML-ENTITIES', 'UTF-8'))); | |
$xpath = new DOMXPath($doc); | |
$query = '//*/meta'; | |
$metas = $xpath->query($query); | |
foreach ($metas as $meta) { | |
$property = $meta->getAttribute('property'); | |
$content = is_string($meta->getAttribute('content')) ? $meta->getAttribute('content') : is_string($meta->getAttribute('property')) ? $meta->getAttribute('property') : ''; | |
if($property) { | |
$rmetas[$property] = $content; | |
} | |
} | |
// formatting the data as required | |
foreach( $doc->getElementsByTagName('meta') as $m){ | |
$name = is_string($m->getAttribute('name')) ? $m->getAttribute('name') : ''; | |
$property = is_string($m->getAttribute('property')) ? $m->getAttribute('property') : ''; | |
$content = is_string($m->getAttribute('content')) ? $m->getAttribute('content') : ''; | |
if($property && (strpos('og:', $property) == 0 || strpos(':', $property) > 0)) { | |
$rmetas[$property] = $content ? $content : ''; | |
} elseif($name && (strpos('og:', $name) == 0 || strpos('twitter:', $name) == 0)) { | |
$rmetas[$name] = $property ? $property : $content; | |
} | |
} | |
// Most of the meta tags get fetched from this method but some tags like facebook with colon are not attained from previous methods so we use default php methods for meta tags | |
$meta_tags = @get_meta_tags($newurl); | |
if(empty($rmetas) && empty($meta_tags)) { | |
$no_data = true; | |
} else { | |
$meta_tags = array_merge($rmetas, is_array($meta_tags) ? $meta_tags : []); | |
} | |
$meta_tags['url'] = $url; | |
$pattern = '/fb.watch|facebook.com/i'; | |
// in case you need to know if the url is facebook video | |
if(preg_match($pattern, $url)) { | |
$is_facebook_url = true; | |
} | |
if(!$no_data || ($no_data && $is_facebook_url)) { | |
$array = [ | |
'status' => true, | |
'all_tags' => $meta_tags, // unformatted data | |
'tags' => $this->getLinkData($meta_tags), // formatted data | |
]; | |
if(empty($array['tags']['title'])) { | |
$array['status'] = false; | |
$array['tags'] = null; | |
$array['all_tags'] = null; | |
} | |
} | |
// saving the data in database for further repetitive fetching | |
$this->PreviewModel->save(['url' => $url, 'json_data' => json_encode($array, true), 'createdDtm' => date('y-m-d H:i:s')]); | |
} | |
} | |
$this->response($array, REST_Controller::HTTP_OK); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment