-1
我從轉換爲json的xml數據製作了一個在線收音機列表。然而,這個腳本使服務器上的cpu達到100%(xml source radioislam.or.id)。我的腳本有什麼問題?CPU使用率達到100%
XML至JSON轉換器
class RII_Data {
private $options = array();
function __construct( $url = '') {
$this->options['url'] = $url;
}
private function get_json($url) {
$xml_string = @file_get_contents($url);
$xml_string = str_replace(array("\n", "\r", "\t"), '', $xml_string);
$xml_string = trim (str_replace('"', "'", $xml_string));
$xml = simplexml_load_string($xml_string);
$json = json_encode($xml);
return $json;
}
private function print_json($json) {
header('Content-Type: application/json');
echo $json;
exit;
}
public function generate_json() {
$json = $this->get_json($this->options['url']);
$this->print_json($json);
}
}
動態獲取無線電的狀態,與此阿賈克斯
function getData() {
$.ajax({
url: './data.php',
type: 'GET',
dataType: 'json',
beforeSend: function() {},
success: function(response) {
console.log(response);
// handle error
if (false === response || $.isEmptyObject(response)) {
// $('#playlist-content').html(loader);
return;
}
//=============//
// response //
//=============//
// status //
// pendengar //
// nama //
// judul //
// info //
// url //
// logo //
//=============//
var radio = response.radio;
// current playing enhancement
if ('' != $('#current-playing').attr('data-info')) {
var currentInfo = $('#current-playing').attr('data-info'),
indexData = findIndex(radio, 'info', currentInfo);
if (null !== indexData) {
$('#current-playing').find('.radio-title > span').html(radio[indexData].judul);
$('#current-playing').find('.radio-status > span.status-listener').html(radio[indexData].pendengar);
}
}
// reset element
$('#playlist-content').html('');
// print
$.each(radio, function(key, data) {
status = data.status.toLowerCase().replace(/ /g, '_');
statusText = '';
if ('live' == status) {
statusText = 'LIVE!';
} else if ('on_air' == status) {
statusText = 'On Air';
} else {
statusText = 'Offline';
}
$('#playlist-content').append('<a class="play-radio clearfix" href="javascript:;" data-info="' + data.info + '" data-stream="' + data.url + '/;stream/1"><img class="radio-logo" width="50" height="50" src="' + data.logo + '"/><div class="radio-name"><i class="fa fa-microphone"></i> <span>' + data.nama + '</span></div>' + '<div class="radio-title">' + data.judul + '</div><div class="radio-status"><i class="fa fa-headphones"></i> <span class="status-listener">' + data.pendengar + '</span><span class="status-stream ' + status + '">' + statusText + '</span></div></a>');
});
// PLAYER GROUP //
$('#play').on('click', function() {
var $first = $('.play-radio').eq(0),
firstStream = $first.data('stream');
$('#jquery_jplayer_1').jPlayer('setMedia', {
mp3: firstStream
}).jPlayer('play');
currentPlaying($first, $first.data('info'));
});
$('.play-radio').each(function(index, element) {
var $this = $(this),
stream = $this.data('stream');
$this.on('click', function() {
$('#jquery_jplayer_1').jPlayer('setMedia', {
mp3: stream
}).jPlayer('play');
currentPlaying($this, $this.data('info'));
});
});
// END PLAYER GROUP //
}
});
}
var refreshData = setInterval(getData, 15000);
getData();
});
data.php
// load class
require_once 'class/rii.class.php';
// get data
$data = new RII_Data('http://radioislam.or.id/apl/list/pri2.xml');
// generate json
$data->generate_json();
爲什麼你認爲什麼是錯的?你是否想要一個不能充分利用可用資源的腳本? –
是的Dagon,我不是一個沒有充分利用資源的腳本。但是,我不是專家。 –