I am trying to get songs history from Shoutcast server using PHP and JQuery. I have performed an ajax call to a php file that contains the code for getting recent played songs from streaming server.The Jquery is written within my application folder
Jquery:
$.ajax({
type: "GET",
url: "http://example.com/recent_songs.php",
headers: {'Access-Control-Allow-Origin': '*'},
success: function(response){
console.log(response);
return false;
},
error: function(error){
console.log(error);
console.log("Network Error!");
}
});
The PHP file is residing on a different server from that of Jquery file.
PHP (recent_songs.php):
$handle = '';
$handle = get_data();
function get_data() {
$ch = curl_init();
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1; rv:31.0) Gecko/20100101 Firefox/31.0' );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 4);
curl_setopt($ch, CURLOPT_TIMEOUT, 8);
$data_string = "";
function write_function($handle, $data) {
global $data_string;
$data_string .= $data;
if (strlen($data_string) > 200) {
return 0;
}
else {
return strlen($data);
}
}
curl_setopt($ch, CURLOPT_URL, 'ip:port/played.html');
$data = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
if ($code!='200'){
return 'error';
}else{
return 'success';
}
It is always returning http code 0 and empty response from shoutcast server. However when I run this url in browser: ip:port/played.html
It shows complete songs history there. Is there any idea to get response from shoutcast server.
Any kind of help will be appreciated. Thanks!
/* Frisky Radio */
$ip='50.7.64.226';
$port=8000;
function get_data( $ip=false, $port=false ) {
try{
if( $ip && $port ){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "{$ip}:{$port}/played.html" );
curl_setopt($ch, CURLOPT_USERAGENT, 'Mozilla/5.0 (Windows NT 5.1; rv:31.0) Gecko/20100101 Firefox/31.0' );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 4);
curl_setopt($ch, CURLOPT_TIMEOUT, 8);
$data=(object)array(
'response' => curl_exec($ch),
'info' => (object)curl_getinfo($ch)
);
return $data;
}
throw new Exception('Ip address and port number are required');
}catch( Exception $e ){
echo $e->getMessage();
}
}
$data=get_data( $ip, $port );
if( $data->info->http_code==200 ){
echo '<pre>',print_r( $data->response,true ),'</pre>';
}
The output of the above ( slightly edited ) is:
02:09:28 FRISKY | RPO Records Session - March 2017 - Oscar Vasquez Current Song
01:09:29 FRISKY | Rogue - July 2017 - Amber Long
00:09:32 FRISKY | Feelin FRISKY - June 2017 - Strachan
22:06:31 FRISKY | Voyager - July 2017 - Deepsense
20:02:07 FRISKY | Fatalist - July 2017 - Digital Department
18:00:47 FRISKY | 6th Auditorium - July 2017 - Andrea Cassino
17:02:07 FRISKY | Perspectives - July 2017 - Paul Kardos
16:02:11 FRISKY | Perspectives - July 2017 - Darin Epsilon
15:06:33 FRISKY | Hazendonk FM - July 2017 - Paul Hazendonk
14:03:26 FRISKY | Southside - July 2017 - Graziano Raffa
Alternatively, with the response data you could use DOMDocument
and DOMXPath
to grab the table displaying the data and use within page somewhere.
So, with the response:
/* Not all shoutcast servers have a list of played songs */
$table='No results';
$dom=new DOMDocument();
$dom->loadHTML( $data->response );
$xp=new DOMXPath( $dom );
$col=$xp->query('//table[2]');
if( !empty( $col ) ){
$doc=new DOMDocument();
$doc->appendChild( $doc->importNode( $col->item( 0 ),true ) );
$table=$doc->saveHTML();
}
$dom = $doc = $xp = $col = null;
echo $table;