title: Downloading youtubevideos to server with php
body: <p>So i want to download youtubevideos as mp3 to my server, so i can listen them easily as mp3.</p>
  
  <p>I have some ecperience in PHP, and second to none in youtube API, but if i have understood correctly youtubevideos are loaded in chunks, and 1 of them is the mp3 soundtrack of the video.</p>
  
  <p>If that is incorrect, could you advice other way to do the downloading?</p>
  
  <p>To this point i have managed to do small php script to get the nessessary information to save the audio.</p>
  
  <pre><code>$id =  $_POST["A"];
  $xmlData = simplexml_load_string(file_get_contents("http://gdata.youtube.com/feeds/api/videos/{$id}?fields=title"));
  
  $title = (string)$xmlData-&gt;title;
  
  $bad = array_merge(
          array_map('chr', range(0,31)),
          array("&lt;", "&gt;", ":", '"', "/", "\\", "|", "?", "*"));
  $result = str_replace($bad, "-", $title);
  
  
  $file = "$result.mp3";
  fopen("$file", 'w+') or die('Cannot open file:  '."$file");
  //echo "http://youtubeinmp3.com/fetch/?video=http://www.youtube.com/watch?v=$id";
  $current = file_get_contents("$file");
  //$current .= "http://youtubeinmp3.com/fetch/?video=http://www.youtube.com/watch?v=$id";
  file_put_contents("$file", $current);
  </code></pre>
  
  <p>(post A is coming from my html part of code and it includes youtube video ID)</p>
  
  <p>So, in that code i dont exactly use youtube to download the mp3 track...
  so it would be great if any of you would know how to download it directly from youtube, and how to save it, because by my logick that code should perfectly work, and it does, untill the saving the audio, it saves about 50 bits and then stops, i dont get any errors or anything so its realy hard to debug.</p>
  
  <p>Also if you notice any errors os better ways to do things in my code pleace notify me.</p>
  
  <p>Also sorry for my bad english i m from finland, and yes sorry for crappy code, this is actualy first practical program i would be going to use :)</p>