PHP cURL Get Files Recursively From External Servers

Published :
Author :
Adam Khoury

Back-end developers can use PHP's cURL functions to get files from external servers. Here we script a recursive loop to get many files all at once in a batch to save many hours of work.

<?php require ('path/to/your_db_connection_file.php'); function curl_fetch_file($url) { // Initialize a cURL session $ch = curl_init(); // Set some options for a cURL transfer curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_URL, $url); // Perform a cURL session $data = curl_exec($ch); // Close a cURL session curl_close($ch); return $data; } $sql = "SELECT id, title, vidid FROM videos ORDER BY id ASC LIMIT 0, 100"; $query = mysqli_query($db_conx, $sql); while($row = mysqli_fetch_array($query, MYSQLI_ASSOC)){ $id = $row["id"]; $title = $row["title"]; $vidid = $row["vidid"]; $imgdata = curl_fetch_file(''.$vidid.'/mqdefault.jpg'); $fpc = 'images/vidthumbs/'.$vidid.'.jpg'; file_put_contents( $fpc, $imgdata ); echo 'Video title - '.$title.'<br>'; echo 'DBID <b>'.$id.'</b> with YTvideoID <b>'.$vidid.'</b> thumbnail received.<br>'; echo '<img src="images/vidthumbs/'.$vidid.'.jpg" width="100"><hr>'; } ?>