Created
September 18, 2023 15:52
-
-
Save MSHADroo/011947ed68c3c9466762b12fea47e118 to your computer and use it in GitHub Desktop.
Wordpress media gallery image downloader from wordpress api
This file contains hidden or 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
set_time_limit(0); | |
$curl = curl_init(); | |
try { | |
for ($page = 1; $page < 100; $page++) { | |
file_put_contents('progress.log', 'page: ' . $page, FILE_APPEND); | |
curl_setopt_array($curl, array( | |
CURLOPT_URL => 'https://www.{DOMAIN}.com/wp-json/wp/v2/media?page=' . $page . '&media_type=image', | |
CURLOPT_RETURNTRANSFER => true, | |
CURLOPT_ENCODING => '', | |
CURLOPT_MAXREDIRS => 10, | |
CURLOPT_TIMEOUT => 5, | |
CURLOPT_FOLLOWLOCATION => true, | |
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, | |
CURLOPT_CUSTOMREQUEST => 'GET', | |
CURLOPT_HTTPHEADER => array( | |
// 'User-Agent: User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36\r\n', | |
'Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7', | |
'Accept-Language: en-US,en;q=0.9', | |
'Cache-Control: max-age=0', | |
'Proxy-Connection: keep-alive', | |
), | |
)); | |
$response = curl_exec($curl); | |
if (!empty($response)) { | |
$res = json_decode($response); | |
if ($res) { | |
$options = [ | |
'http' => [ | |
'header' => "User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/93.0.4577.63 Safari/537.36\r\n" | |
] | |
]; | |
$context = stream_context_create($options); | |
foreach ($res as $media) { | |
// var_dump($media->media_details->file); | |
$parts = explode('/', $media->media_details->file); | |
$last = array_pop($parts); | |
if (!file_exists(implode(DIRECTORY_SEPARATOR, $parts))) { | |
mkdir(implode(DIRECTORY_SEPARATOR, $parts), 0755, true); | |
} | |
file_put_contents($media->media_details->file, file_get_contents($media->source_url, false, $context)); | |
} | |
} | |
} | |
sleep(1); | |
} | |
echo 'Done'; | |
} catch (\Exception $e) { | |
var_dump($e->getMessage(), $e->getLine()); | |
} | |
curl_close($curl); | |
die(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment