0

I want to store some data retrieved using an API on my server. Specifically, these are .mp3 files of (free) learning tracks. I'm running into a problem though. The mp3 link returned from the request isn't to a straight .mp3 file, but rather makes an ADDITIONAL API call which normally would prompt you to download the mp3 file.

file_put_contents doesn't seem to like that. The mp3 file is empty.

Here's the code:

$id = $_POST['cid'];
$title = $_POST['title'];

if (!file_exists("tags/".$id."_".$title))
{
    mkdir("tags/".$id."_".$title);
}
else
echo "Dir already exists";

file_put_contents("tags/{$id}_{$title}/all.mp3", fopen($_POST['all'], 'r'));

And here is an example of the second API I mentioned earlier: http://www.barbershoptags.com/dbaction.php?action=DownloadFile&dbase=tags&id=31&fldname=AllParts

Is there some way to bypass this intermediate step? If there's no way to access the direct URL of the mp3, is there a way to redirect the file download prompt to my server?

Thank you in advance for your help!

EDIT

Here is the current snippet. I should be echoing something, correct?

$handle = fopen("http://www.barbershoptags.com/dbaction.php?action=DownloadFile&dbase=tags&id=31&fldname=AllParts", 'rb');
$contents = stream_get_contents($handle);
echo $contents;

Because this echos nothing.

SOLUTION

Ok, I guess file_get_contents is supposed to handle redirects just fine, but this wasn't happening. So I found this function: https://stackoverflow.com/a/4102293/2723783 to return the final redirect of the API. I plugged that URL into file_get_contents and volia!

Community
  • 1
  • 1
Nick
  • 19
  • 6

1 Answers1

0

You seem to be just opening the file handler and not getting the contents using fread() or another similar function:

http://www.php.net/manual/en/function.fread.php

$handle = fopen($_POST['all'], 'rb')
file_put_contents("tags/{$id}_{$title}/all.mp3", stream_get_contents($handle));
Devon Bessemer
  • 34,461
  • 9
  • 69
  • 95
  • Thanks for the response, and I think this is in the right direction, but I've tried stream_get_contents and fread both to no avail. I can't even seem to echo the contents. Everything returns blank. – Nick Apr 16 '14 at 17:11
  • Do you get any errors? Place `error_reporting(E_ALL);` at the top. – Devon Bessemer Apr 16 '14 at 17:13
  • stream_get_contents gives me no errors, but if I try to use fread, the filesize stat fails. I plugged in an arbitrary value for filesize just for kicks, but that didn't fix it. – Nick Apr 16 '14 at 17:18
  • Code works fine. I just tried it on my server. I plugged in the URL you provided for `$_POST['all']` – Devon Bessemer Apr 16 '14 at 17:22
  • Thanks for testing it. If you can get the audio file playing, then it must be something on my end. I'll take a closer look. Thanks again. – Nick Apr 16 '14 at 17:26
  • I didn't check if it played but it was ~320KB. Not empty filesize as you said. – Devon Bessemer Apr 16 '14 at 17:27
  • Right. Yeah I'm getting 0kb. – Nick Apr 16 '14 at 17:29
  • I replaced it with the URL to make sure. I'll paste my current code above. – Nick Apr 16 '14 at 17:36