Mj.
Mj.

Reputation: 331

How to convert mp3 format data to mp3 file using php?

I am making PHP curl call and getting mp3 format raw data. How do i convert that raw data to mp3 file. My code is:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:audio/mpeg'));
curl_setopt($ch, CURLOPT_USERPWD, $username.":".$password);
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$data=curl_exec ($ch);

I received raw data of mp3 format. i don't want to download that file in browser, i want to save that file in my server side. How do i convert that raw data to mp3 format file?

I tried normal php file writing but it not works. for example :

$myfile = fopen("D:\xampp\htdocs\abc\testfile.mp3", "w"); 
$fwrite($myfile, $data);

fopen returns false value and I could not create mp3 file using php file operation. How do i convert and save the mp3 file in server??. after saving that file i will give that path to audio tag src. <audio src="D:\xampp\htdocs\abc\testfile.mp3" />

my curl response like this:

ID3#TSSELavf56.15.102��8�Infora !#%*,.0357<>@BEGIMPRTVY[adfhjmqsuxz|~��������������������������������������������������Lavc56.13$a5Cwc��8�"#�p7I<��Z���<ѣF� ��HP���.uN{P�! �{�����������]�D�DDwww��������"'����_���";�������=��B""�������

Upvotes: 0

Views: 1384

Answers (1)

DQM
DQM

Reputation: 552

You forget the binary transfer directive:

 curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);

Upvotes: 3

Related Questions