Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Replace urllib.request urlopen in PHP

I have a URL which can not download using video file using PHP. I have to run it ones in browser so that code can start downloading videos from it.

I have tried using curl, it fails to download video on first hit to url through code.
I have tried using Ajax call by creating video blob, it works however because of cron job, javascript can not work.

I need solution in PHP.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Use Guzzle in PHP

   require_once('guzzle/vendor/autoload.php');

   use GuzzleHttp\Client;
   use GuzzleHttp\Exception\RequestException;

   $client = new Client();
   $url = 'video url'; // URL of the file to download
   $file_name = 'filename.mp4';
   $saveTo = $save_file_loc = __DIR__."/".$file_name; // Local path to save the file

   try {
       $response = $client->request('GET', $url, ['sink' => $saveTo]);
       echo "File downloaded successfully to " . $saveTo;
   } catch (RequestException $e) {
       echo "Error: " . $e->getMessage();
       if ($e->hasResponse()) {
           echo "\nHTTP Status Code: " . $e->getResponse()->getStatusCode();
       }
   }
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading