Jump to content

Faster way to get external audio file


cataiin

Recommended Posts

Hello.

I have a (big) text and a JSON file containing url needed. But execution time is toooo long.

$texts returns string(50) "Lorem ipsum dolor sit amet, consectetur adipiscing" string(50) "elit. Cras diam velit, lobortis nec fermentum id, " string(46) "congue nec nibh. Pellentesque pretium, dui ut."

$text_encode = rawurlencode($texts);
$url_address = @file_get_contents('http://api.url/?text='.$text_encode);

http://api.url/?text=something it's a JSON result. So I use this to select and get .mp3 file:

$before_address = '"url":"';
$after_address = '","format"';
$address = strstr(substr($url_address, strpos($url_address, $before_address) + strlen($before_address)), $after_address, true);
$mp3 = @file_get_contents($address);

and to save...

$part = 1;
$files = 'audio/parts/'.md5($mp3).'-'.$part.'.mp3';
$file_final = '/audio/'.md5($mp3)'.mp3';
$save_mp3 = file_put_contents($files, $mp3);
$save_mp3_final = file_put_contents($file_final, $mp3, FILE_APPEND);

It's file_get_contents a bad practice in both cases? Or file_put_contents used two times? What should I use to retrieve data faster...? Thanks.

 

I've been thinking about json_decode and cURL, but never used and I don't know if it's best method to solve my problem.

Edited by cataiin
Link to comment
Share on other sites

It's difficult to really provide any advise since we do not know what data is returned. What is the size of the data returned for $url_address and $mp3? Why do you need to need to get the data in that manner in the first place? Are you scraping the content of another site (not your own)?

 

You are reading at least four different pages/URLs that I can see. You will have to deal with the latency in doing that and there is little that could be done if you don't have any other means of getting the data you are after. The file_put_contents() shouldn't be more of an issue than the file_get_contents().

 

However, the line to get the address could be slightly improved, but I highly doubt it is the source of your problem

$startPos = strpos($url_address, $before_address) + strlen($before_address);
$endPos = strpos($url_address, $after_address);
$address = substr($url_address, $startPos, $endPos-$startPos);

May need to add or subtract 1 from the last parameter in substr() - I was too lazy to test.

Edited by Psycho
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.