false, 'error' => 'Please enter a URL']; } if (!filter_var($url, FILTER_VALIDATE_URL)) { return ['success' => false, 'error' => 'Invalid URL format']; } // Detect platform $platform = detectPlatform($url); if ($platform === 'unknown') { return ['success' => false, 'error' => 'Unsupported platform. Please check the URL.']; } // Process the download based on platform try { switch ($platform) { case 'youtube': return downloadYouTube($url); case 'instagram': return downloadInstagram($url); case 'tiktok': return downloadTikTok($url); case 'facebook': return downloadFacebook($url); case 'twitter': return downloadTwitter($url); case 'pinterest': return downloadPinterest($url); default: return fetchMediaInfo($url, $platform); } } catch (Exception $e) { return ['success' => false, 'error' => 'Error processing request: ' . $e->getMessage()]; } } function detectPlatform($url) { $host = parse_url(strtolower($url), PHP_URL_HOST); if (strpos($host, 'youtube.com') !== false || strpos($host, 'youtu.be') !== false) { return 'youtube'; } elseif (strpos($host, 'instagram.com') !== false || strpos($host, 'instagr.am') !== false) { return 'instagram'; } elseif (strpos($host, 'facebook.com') !== false || strpos($host, 'fb.com') !== false || strpos($host, 'fb.watch') !== false) { return 'facebook'; } elseif (strpos($host, 'twitter.com') !== false || strpos($host, 'x.com') !== false) { return 'twitter'; } elseif (strpos($host, 'tiktok.com') !== false || strpos($host, 'vm.tiktok.com') !== false) { return 'tiktok'; } elseif (strpos($host, 'pinterest.com') !== false || strpos($host, 'pin.it') !== false) { return 'pinterest'; } return 'unknown'; } // YouTube Download Function (No API Key Required) function downloadYouTube($url) { $videoId = extractYouTubeId($url); if (!$videoId) { return ['success' => false, 'error' => 'Invalid YouTube URL']; } // Get video info using oEmbed (no API key required) $oEmbedUrl = "https://www.youtube.com/oembed?url=https://www.youtube.com/watch?v={$videoId}&format=json"; $response = @file_get_contents($oEmbedUrl); if ($response === FALSE) { // Fallback to basic info return getMockYouTubeData($videoId); } $data = json_decode($response, true); return [ 'success' => true, 'platform' => 'youtube', 'title' => $data['title'] ?? 'YouTube Video', 'thumbnail' => "https://img.youtube.com/vi/{$videoId}/maxresdefault.jpg", 'duration' => 'N/A', 'author' => $data['author_name'] ?? 'YouTube Channel', 'video_id' => $videoId, 'formats' => [ ['id' => 'mp4_720', 'quality' => '720p', 'format' => 'MP4', 'size' => '28.7 MB', 'type' => 'video'], ['id' => 'mp4_480', 'quality' => '480p', 'format' => 'MP4', 'size' => '15.3 MB', 'type' => 'video'], ['id' => 'mp3', 'quality' => 'Audio', 'format' => 'MP3', 'size' => '5.3 MB', 'type' => 'audio'] ] ]; } function extractYouTubeId($url) { $pattern = '/(?:youtube\.com\/(?:[^\/]+\/.+\/|(?:v|e(?:mbed)?)\/|.*[?&]v=)|youtu\.be\/)([^"&?\/\s]{11})/'; preg_match($pattern, $url, $matches); return isset($matches[1]) ? $matches[1] : null; } // Instagram Download Function with Multiple API Options function downloadInstagram($url) { // Try multiple API endpoints $apis = [ [ 'url' => "https://instagram-scraper-api2.p.rapidapi.com/v1/media_info?url=" . urlencode($url), 'headers' => [ "X-RapidAPI-Key: YOUR_INSTAGRAM_API_KEY_HERE", "X-RapidAPI-Host: instagram-scraper-api2.p.rapidapi.com" ] ], [ 'url' => "https://instagram-downloader-download-instagram-videos-stories.p.rapidapi.com/index?url=" . urlencode($url), 'headers' => [ "X-RapidAPI-Key: YOUR_INSTAGRAM_API_KEY_HERE", "X-RapidAPI-Host: instagram-downloader-download-instagram-videos-stories.p.rapidapi.com" ] ] ]; foreach ($apis as $api) { $result = makeApiRequest($api['url'], $api['headers']); if ($result['success']) { $data = $result['data']; if (isset($data['graphql']['shortcode_media']) || isset($data['items'][0])) { $media = isset($data['graphql']['shortcode_media']) ? $data['graphql']['shortcode_media'] : $data['items'][0]; $isVideo = $media['is_video'] ?? false; $videoUrl = $media['video_url'] ?? null; return [ 'success' => true, 'platform' => 'instagram', 'title' => 'Instagram ' . ($isVideo ? 'Video' : 'Post'), 'thumbnail' => $media['display_url'] ?? ($media['thumbnail_src'] ?? 'https://img.icons8.com/color/480/instagram-new.png'), 'duration' => $isVideo ? '0:30' : 'N/A', 'author' => $media['owner']['username'] ?? 'instagram_user', 'formats' => [ ['id' => 'original', 'quality' => 'Original', 'format' => $isVideo ? 'MP4' : 'JPG', 'size' => '12.8 MB', 'type' => $isVideo ? 'video' : 'image'] ] ]; } } } // If all APIs fail, return mock data return getMockInstagramData(); } // TikTok Download Function function downloadTikTok($url) { $apis = [ [ 'url' => "https://tiktok-downloader-download-tiktok-videos-without-watermark.p.rapidapi.com/vid/index?url=" . urlencode($url), 'headers' => [ "X-RapidAPI-Key: YOUR_TIKTOK_API_KEY_HERE", "X-RapidAPI-Host: tiktok-downloader-download-tiktok-videos-without-watermark.p.rapidapi.com" ] ], [ 'url' => "https://tiktok-video-no-watermark2.p.rapidapi.com/?url=" . urlencode($url) . "&hd=1", 'headers' => [ "X-RapidAPI-Key: YOUR_TIKTOK_API_KEY_HERE", "X-RapidAPI-Host: tiktok-video-no-watermark2.p.rapidapi.com" ] ] ]; foreach ($apis as $api) { $result = makeApiRequest($api['url'], $api['headers']); if ($result['success']) { $data = $result['data']; if (isset($data['video']) || isset($data['data'])) { $videoData = isset($data['video']) ? $data : $data['data']; return [ 'success' => true, 'platform' => 'tiktok', 'title' => $videoData['title'] ?? 'TikTok Video', 'thumbnail' => $videoData['cover'] ?? ($videoData['cover_url'] ?? 'https://img.icons8.com/color/480/tiktok.png'), 'duration' => '0:30', 'author' => $videoData['author']['nickname'] ?? ($videoData['author'] ?? '@tiktok_user'), 'formats' => [ ['id' => 'no_watermark', 'quality' => 'No Watermark', 'format' => 'MP4', 'size' => '9.1 MB', 'type' => 'video'], ['id' => 'watermark', 'quality' => 'With Watermark', 'format' => 'MP4', 'size' => '8.5 MB', 'type' => 'video'] ] ]; } } } return getMockTikTokData(); } // Facebook Download Function function downloadFacebook($url) { $apis = [ [ 'url' => "https://facebook-reel-and-video-downloader.p.rapidapi.com/app/main.php?url=" . urlencode($url), 'headers' => [ "X-RapidAPI-Key: YOUR_FACEBOOK_API_KEY_HERE", "X-RapidAPI-Host: facebook-reel-and-video-downloader.p.rapidapi.com" ] ] ]; foreach ($apis as $api) { $result = makeApiRequest($api['url'], $api['headers']); if ($result['success']) { $data = $result['data']; if (isset($data['links']) && count($data['links']) > 0) { $formats = []; foreach ($data['links'] as $index => $link) { $formats[] = [ 'id' => 'quality_' . $index, 'quality' => $link['quality'] ?? 'HD', 'format' => 'MP4', 'size' => '25.6 MB', 'type' => 'video' ]; } return [ 'success' => true, 'platform' => 'facebook', 'title' => $data['title'] ?? 'Facebook Video', 'thumbnail' => $data['thumbnail'] ?? 'https://img.icons8.com/color/480/facebook.png', 'duration' => '2:15', 'author' => $data['author'] ?? 'Facebook User', 'formats' => $formats ]; } } } return getMockFacebookData(); } // Twitter Download Function function downloadTwitter($url) { $apis = [ [ 'url' => "https://twitter-downloader-download-twitter-videos-gifs-and-images.p.rapidapi.com/status?url=" . urlencode($url), 'headers' => [ "X-RapidAPI-Key: YOUR_TWITTER_API_KEY_HERE", "X-RapidAPI-Host: twitter-downloader-download-twitter-videos-gifs-and-images.p.rapidapi.com" ] ] ]; foreach ($apis as $api) { $result = makeApiRequest($api['url'], $api['headers']); if ($result['success']) { $data = $result['data']; if (isset($data['media']) && count($data['media']) > 0) { $media = $data['media'][0]; $isVideo = isset($media['video']); $formats = []; if ($isVideo && isset($media['video']['variants'])) { foreach ($media['video']['variants'] as $index => $variant) { if (strpos($variant['content_type'], 'video') !== false) { $formats[] = [ 'id' => 'video_' . $index, 'quality' => 'Video ' . ($index + 1), 'format' => 'MP4', 'size' => '15.7 MB', 'type' => 'video' ]; } } } else { $formats[] = [ 'id' => 'image', 'quality' => 'Original', 'format' => 'JPG', 'size' => '8.2 MB', 'type' => 'image' ]; } return [ 'success' => true, 'platform' => 'twitter', 'title' => $data['text'] ?? 'Twitter Post', 'thumbnail' => $media['media_url_https'] ?? 'https://img.icons8.com/color/480/twitter.png', 'duration' => $isVideo ? '0:45' : 'N/A', 'author' => $data['user']['screen_name'] ?? '@twitter_user', 'formats' => $formats ]; } } } return getMockTwitterData(); } // Pinterest Download Function (No API Required) function downloadPinterest($url) { // Simple scraping approach for Pinterest $html = @file_get_contents($url); if ($html !== FALSE) { // Extract image URL from Pinterest page preg_match('/ true, 'platform' => 'pinterest', 'title' => $title, 'thumbnail' => $imageUrl, 'duration' => 'N/A', 'author' => 'Pinterest User', 'formats' => [ ['id' => 'original', 'quality' => 'Original', 'format' => 'JPG', 'size' => '5.2 MB', 'type' => 'image'], ['id' => 'hd', 'quality' => 'HD', 'format' => 'JPG', 'size' => '3.8 MB', 'type' => 'image'] ] ]; } } return getMockPinterestData(); } // Universal API Request Function with Better Error Handling function makeApiRequest($url, $headers = []) { $context = stream_context_create([ 'http' => [ 'method' => 'GET', 'header' => implode("\r\n", $headers), 'timeout' => 10 ], 'ssl' => [ 'verify_peer' => false, 'verify_peer_name' => false ] ]); try { $response = @file_get_contents($url, false, $context); if ($response === FALSE) { $error = error_get_last(); return [ 'success' => false, 'error' => $error['message'] ?? 'Unknown error' ]; } $data = json_decode($response, true); if (json_last_error() !== JSON_ERROR_NONE) { return [ 'success' => false, 'error' => 'Invalid JSON response' ]; } return [ 'success' => true, 'data' => $data ]; } catch (Exception $e) { return [ 'success' => false, 'error' => $e->getMessage() ]; } } // Mock data functions as fallback function getMockYouTubeData($videoId = null) { return [ 'success' => true, 'platform' => 'youtube', 'title' => 'YouTube Video', 'thumbnail' => 'https://img.icons8.com/color/480/youtube-play.png', 'duration' => '10:30', 'author' => 'YouTube Channel', 'formats' => [ ['id' => 'mp4_720', 'quality' => '720p', 'format' => 'MP4', 'size' => '28.7 MB', 'type' => 'video'], ['id' => 'mp4_480', 'quality' => '480p', 'format' => 'MP4', 'size' => '15.3 MB', 'type' => 'video'] ] ]; } function getMockInstagramData() { return [ 'success' => true, 'platform' => 'instagram', 'title' => 'Instagram Post', 'thumbnail' => 'https://img.icons8.com/color/480/instagram-new.png', 'duration' => '0:15', 'author' => '@instagram_user', 'formats' => [ ['id' => 'original', 'quality' => 'Original', 'format' => 'MP4', 'size' => '12.8 MB', 'type' => 'video'] ] ]; } function getMockTikTokData() { return [ 'success' => true, 'platform' => 'tiktok', 'title' => 'TikTok Video', 'thumbnail' => 'https://img.icons8.com/color/480/tiktok.png', 'duration' => '0:30', 'author' => '@tiktok_user', 'formats' => [ ['id' => 'no_watermark', 'quality' => 'No Watermark', 'format' => 'MP4', 'size' => '9.1 MB', 'type' => 'video'] ] ]; } function getMockFacebookData() { return [ 'success' => true, 'platform' => 'facebook', 'title' => 'Facebook Video', 'thumbnail' => 'https://img.icons8.com/color/480/facebook.png', 'duration' => '2:15', 'author' => 'Facebook User', 'formats' => [ ['id' => 'hd', 'quality' => 'HD', 'format' => 'MP4', 'size' => '25.6 MB', 'type' => 'video'] ] ]; } function getMockTwitterData() { return [ 'success' => true, 'platform' => 'twitter', 'title' => 'Twitter Video', 'thumbnail' => 'https://img.icons8.com/color/480/twitter.png', 'duration' => '0:45', 'author' => '@twitter_user', 'formats' => [ ['id' => 'original', 'quality' => 'Original', 'format' => 'MP4', 'size' => '15.7 MB', 'type' => 'video'] ] ]; } function getMockPinterestData() { return [ 'success' => true, 'platform' => 'pinterest', 'title' => 'Pinterest Pin', 'thumbnail' => 'https://img.icons8.com/color/480/pinterest.png', 'duration' => 'N/A', 'author' => 'Pinterest User', 'formats' => [ ['id' => 'original', 'quality' => 'Original', 'format' => 'JPG', 'size' => '5.2 MB', 'type' => 'image'] ] ]; } function fetchMediaInfo($url, $platform) { $mockData = [ 'default' => [ 'title' => 'Social Media Content', 'thumbnail' => 'https://img.icons8.com/color/480/video.png', 'duration' => 'N/A', 'author' => 'Unknown', 'formats' => [ ['id' => '1', 'quality' => 'Original', 'format' => 'MP4', 'size' => '10.5 MB', 'type' => 'video'] ] ] ]; $data = $mockData['default']; $data['platform'] = $platform; $data['success'] = true; return $data; } function generateDownloadLink($downloadData) { sleep(1); // Simulate processing time $filename = "download_" . time() . "_" . $downloadData['quality'] . "." . strtolower($downloadData['format']); // For demo purposes - in production, this would serve the actual file $downloadUrl = "javascript:alert('Download would start for: " . $filename . "')"; return [ 'success' => true, 'download_url' => $downloadUrl, 'filename' => $filename, 'message' => 'Download ready!' ]; } // Get current URL for form action $current_url = "http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; ?> Social Media Downloader | BD Exchange

Download Videos & Images from Social Media

Free tool to download videos, images, and stories from popular social media platforms. No registration required!

API Setup Instructions

  1. Go to RapidAPI.com and create an account
  2. Subscribe to these APIs:
    • Instagram Downloader
    • TikTok Downloader
    • Facebook Video Downloader
    • Twitter Video Downloader
  3. Copy your API keys and replace the placeholders in the PHP code
  4. Save the file and test with a social media URL

Note: YouTube and Pinterest work without API keys!

YouTube
Instagram
TikTok
Facebook
Twitter

Processing your request...

Download Options

Media preview

Media Title

Platform: Unknown

Duration: N/A

Author: Unknown