Skip to content

Instantly share code, notes, and snippets.

@asrorbekh
Created August 9, 2024 19:35
Show Gist options
  • Save asrorbekh/2f80b36beab509ea0ea0f5ff997a368a to your computer and use it in GitHub Desktop.
Save asrorbekh/2f80b36beab509ea0ea0f5ff997a368a to your computer and use it in GitHub Desktop.
<?php
require 'vendor/autoload.php';
use Curl\Curl;
class InstagramDownloader
{
private Curl $curl;
private string|null $url;
public function __construct(string|null $url = null)
{
$this->curl = new Curl();
$this->url = $url;
}
public function download(): array
{
$this->curl->setHeader('Content-Type', 'application/x-www-form-urlencoded');
$this->curl->setHeader(
'User-Agent',
'Mozilla/5.0 (Linux; Android 10; K) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Mobile Safari/537.36'
);
$this->curl->setHeader('Referer', 'https://instasave.website/download');
$this->curl->post('https://api.instasave.website/media', [
'url' => $this->url,
'lang' => 'en'
]);
if ($this->curl->error) {
return ["error" => $this->curl->errorMessage];
}
preg_match_all('/"https:\/\/\S*?"/', $this->curl->response, $matches);
$photo = isset($matches[0][0]) ? trim($matches[0][0], '"') : null;
$video = isset($matches[0][1]) ? trim($matches[0][1], '"') : null;
return [
'video' => $video,
'photo' => $photo,
'matches' => $matches
];
}
public function __destruct()
{
$this->curl->close();
}
}
header("Content-Type: application/json; charset=UTF-8");
if (!isset($_GET['url'])) {
echo json_encode(["error" => "Invalid URL!"], JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
} else {
$downloader = new InstagramDownloader($_GET['url']);
$data = $downloader->download();
echo json_encode($data, JSON_UNESCAPED_UNICODE | JSON_PRETTY_PRINT | JSON_UNESCAPED_SLASHES);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment