How to Download Facebook Video Using PHP

In this tutorial I am going to share simple PHP library written by vikash to download facebook video in Low and HD quality. You only need to pass facebook video URL and you’ll able to get facebook video download link in normal quality and MP4 HD Quality if available.
Facebook Video Down loader Script

Setting HTTP header array and get facebook video URL content.

$url = 'Facebook Video Link';
$context = [
    'http' => [
        'method' => 'GET',
        'header' => "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/51.0.2704.47 Safari/537.36",
    ],
];
 
$context = stream_context_create($context);
$data = file_get_contents($url, false, $context);

Get facebook MP4 HD Video download link.

$regex = '/hd_src_no_ratelimit:"([^"]+)"/';
    if (preg_match($regex, $data, $match)) {
        echo $linkHD =  $match[1];
    }


Get facebook MP4 Low quality Video download link.

$regex = '/sd_src_no_ratelimit:"([^"]+)"/';
    if (preg_match($regex, $data, $match)) {
         echo $linkSD =  $match[1];
    }

Get Facebook Video Title.

   $title = '';
    if (preg_match('/h2 class="uiHeaderTitle"?[^>]+>(.+?)<\/h2>/', $data, $matches)) {
        $title = $matches[1];
    } elseif (preg_match('/title id="pageTitle">(.+?)<\/title>/', $data, $matches)) {
        $title = $matches[1];
}
echo $videoTitle = html_entity_decode(strip_tags($title), ENT_QUOTES, 'UTF-8');

See live demo and download source code.

DEMO | DOWNLOAD

Visit official github repository for more information and follow for future updates. Don’t forget to read license for using this plugin in your projects.

Posted in: PHP

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.