buddhika2010 Posted December 22, 2010 Share Posted December 22, 2010 I have a small mp3 script and its running accurately ...i want to check the mp3 URL before playing it whether the mp3 link active or broken ....how to check the link is active or broken in php thx Quote Link to comment https://forums.phpfreaks.com/topic/222349-help-in-php-coding/ Share on other sites More sharing options...
requinix Posted December 22, 2010 Share Posted December 22, 2010 You can send a HEAD request, with cURL or a socket, and check for a non-200 (at the very least not a 404) response. Quote Link to comment https://forums.phpfreaks.com/topic/222349-help-in-php-coding/#findComment-1150141 Share on other sites More sharing options...
buddhika2010 Posted December 22, 2010 Author Share Posted December 22, 2010 can u provide a sample code ....i have no idea Quote Link to comment https://forums.phpfreaks.com/topic/222349-help-in-php-coding/#findComment-1150146 Share on other sites More sharing options...
QuickOldCar Posted December 22, 2010 Share Posted December 22, 2010 Made this up just for you and tested it even. <?php function checkLiveMp3($url){ $context = stream_context_create(array( 'http' => array( 'timeout' => 1 ) )); $the_contents = @file_get_contents($url, 0, $context); if (empty($the_contents)) { echo "$url is Dead<br />"; echo "Insert some code here for an error or other code.<br />"; echo "<br /><hr>"; } else { $mp3_check = substr("$url", -4); if ($mp3_check != ".mp3") { echo "$url is alive but not mp3 file<br />"; echo "Insert some code here for an error or other code.<br />"; echo "<br /><hr>"; } else { echo "Alive and mp3 file<br />"; echo "Insert some code here so some music can play.<br />"; echo "Mp3: <a href='$url'>$url</a>"; echo "<br /><hr>"; } } } ?> <?php //Usage $url = "http://www.wyntonmarsalis.org/audio/tribute/wyntonstribute.mp3"; checkLiveMp3($url); checkLiveMp3("http://somesite.com/something.jpg"); checkLiveMp3("http://www.sotone.com/samples/!2-Shapiro_Brahms.1st.mvmt.mp3"); ?> Quote Link to comment https://forums.phpfreaks.com/topic/222349-help-in-php-coding/#findComment-1150219 Share on other sites More sharing options...
buddhika2010 Posted December 22, 2010 Author Share Posted December 22, 2010 thanks a lot Quote Link to comment https://forums.phpfreaks.com/topic/222349-help-in-php-coding/#findComment-1150224 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.