Skip to content

Instantly share code, notes, and snippets.

@afzafri
Created November 23, 2020 06:49
Show Gist options
  • Save afzafri/93b3f53b8b80abffbca6ccf46a5a5d2b to your computer and use it in GitHub Desktop.
Save afzafri/93b3f53b8b80abffbca6ccf46a5a5d2b to your computer and use it in GitHub Desktop.
Scrape Facebook Video Thumbnail
<?php
$video_url = $_GET['video_url'];
echo get_fb_thumbnail($video_url);
function get_fb_thumbnail($video_url) {
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => $video_url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => "",
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => "GET",
CURLOPT_HTTPHEADER => array(
"User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:65.0) Gecko/20100101 Firefox/65.0",
"Cookie: fr=1ptzcnDkjdXv2IfZZ..Bfu1P7.es.AAA.0.0.Bfu1Qo.AWVKu_WtXv8"
),
));
$response = curl_exec($curl);
curl_close($curl);
$thumbnail_src = "";
if (preg_match('#<video [^>]+></video>\s*<div [^>]+><img [^>]+src="([^"]+)#s',$response,$matches))
{
$image = $matches[1];
$image = str_replace('&amp;','&',$image);
if (strpos($image,'&')) {
$thumbnail_src = $image;
}
}
return $thumbnail_src;
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment