Sunday, January 21, 2018

Extract MP3 Songs Link in PHP

This tutorial explain how to extract MP3 songs from particular websiteNowadays it is a common and useful technique to extract data from another server. By this way you can extract useful data like meta tag details, html attribute/ tags details.

Check out our blog archive on the topic if you’re looking to learn about PHP CURL
In case you want to learn more about link extraction technique using PHP CURL.

Lets see the source code to extract MP3 Links :
Here we have used file_get_contents() function to extract information from particular link. 
 error_reporting(E_ERROR | E_PARSE);
 $URL = "";
 $str = file_get_contents($URL);
 $dom = new DOMDocument;
 foreach ($dom->getElementsByTagName('a') as $tag) {
            echo $tag->getAttribute('href')."<br>";

Output :

Check out another example to download MP3 Songs.
// script to download  mp3 songs
$url = '' ;
//This is the file where we save the    information
$fp = fopen (dirname(__FILE__) . '/audio.mp3', 'w+');
//Here is the file we are downloading, replace spaces with %20
$ch = curl_init(str_replace(" ","%20",$url));
curl_setopt($ch, CURLOPT_TIMEOUT, 50);
// write curl response to file
curl_setopt($ch, CURLOPT_FILE, $fp); 
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
// get curl response