cataiin Posted March 23, 2014 Share Posted March 23, 2014 (edited) 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 March 23, 2014 by cataiin Quote Link to comment Share on other sites More sharing options...
cataiin Posted March 24, 2014 Author Share Posted March 24, 2014 Nobody? If you don't understand please ask, my english is kinda bad. Quote Link to comment Share on other sites More sharing options...
Psycho Posted March 24, 2014 Share Posted March 24, 2014 (edited) 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 March 24, 2014 by Psycho Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.