Download video and music using PHP

Using our API to download YouTube video, SoundCloud music, Facebook media, etc with PHP. For FREE!

Code Example

PHP


This API is really easy to use to download media files! Check this example out. How to download a YouTube video with PHP:

// This code snippet uses an open-source library. http://unirest.io/php
$response = Unirest\Request::get("https://getvideo.p.mashape.com/?url=PAGE-WITH-MEDIA",
  array(
    "X-Mashape-Key" => "YOUR-API-KEY",
    "Accept" => "text/plain"
  )
);

This will return a JSON array with download links, thumbnails, etc!

As an example, imagine you want to download the trailer for Serenity, the okay movie based on the awesome series Firefly, from YouTube. First, locate the URL you want, in this case it’s “https://www.youtube.com/watch?v=KWP2ZVDPkMo”.

Then you can just use that URL (with URL encoding), and your Mashape API key, as above. If you don’t have an API key yet, click here to get started.

So, your PHP code should look something like this:

// This code snippet uses an open-source library. http://unirest.io/php
$response = Unirest\Request::get("https://getvideo.p.mashape.com/?url=https%3A%2F%2Fwww.youtube.com%2Fwatch%3Fv%3DKWP2ZVDPkMo",
  array(
    "X-Mashape-Key" => "",
    "Accept" => "text/plain"
  )
);
You will get a JSON array back as a result:
 {
   "status":true,
   "message":"Successfully received info.",
   "site":"Youtube",
   "title":"Serenity - Trailer", 
   "like_count":52,
   "dislike_count":7,
   "view_count":79776,
   "duration":140,
   "upload_date":"20131211",
   "description":"A passenger with a deadly secret. Six rebels on the run. An assassin in pursuit. When the renegade crew of Serenity agrees to hide a fugitive on their ship, they find themselves in an action-packed battle between the relentless military might of a totalitarian regime who will destroy anything - or anyone - to get the girl back and the bloodthirsty creatures who roam the uncharted areas of space. But, the greatest danger of all may be on their ship. From the mind of Joss Whedon (Buffy the Vampire Slayer, Angel) comes a new edge-of-your-seat adventure loaded with explosive battles, gripping special effects and fantastic new worlds!",
   "tags":[
      "Joss Whedon",
      "Nathan Fillion",
      "Alan Tudyk",
      "Gina Torres",
      "Morena Baccarin",
      "Adam Baldwin",
      "Jewel Staite",
      "Reavers"],
   "uploader":"UniversalMoviesINTL",
   "uploader_url":"http:\/\/www.youtube.com\/user\/UniversalMoviesINTL",
   "thumbnail":"https:\/\/i.ytimg.com\/vi\/KWP2ZVDPkMo\/maxresdefault.jpg",
   "streams":[
   {
      "url":"https:\/\/r6---sn-4g57knkz.googlevideo.com\/videoplayback?id=9d[...]",
      "format":"audio only (DASH audio)",
      "format_note":"Audio only, no video",
      "extension":"m4a",
      "video_codec":"none",
      "audio_codec":"mp4a.40.5",
      "filesize":1438229,
      "height":null,
      "width":null
   },
   {
      "url":"https:\/\/r6---sn-4g57knkz.googlevideo.com\/videoplayback?key=[...]",
      "format":"1920x1080 (1080p)",
      "format_note":"1080p",
      "extension":"webm",
      "video_codec":"vp9",
      "audio_codec":"none",
      "filesize":68016821,
      "height":1080,
      "width":1920
   },
  {
      ...
   }

In the resulting JSON array, you have “status” and “message” to give you the results, the thumbnail for the video, and the most important part, the “streams” array!

The “streams” array contains all variants of the video, with or without audio, and in different quality!

Just pick the URL you want to download, and that’s it!

Developers are downloading 50,000+ videos daily with our API. For FREE!