Jump to Product

How to grab versions from XML

Very simple, here you've an axample about how we grab the latest version for our AceSEF component.
$latest_version == '?.?.?';
$content = self::getRemoteData('http://www.joomace.net/index.php?option=com_aceversions&view=xml&format=xml');

if (strstr($content, '')) {
$xml =& JFactory::getXMLparser('Simple');
$xml->loadString($content);

foreach ($xml->document->children() as $category) {
$catname = $category->attributes('name');
if ($catname == 'components') {
foreach ($category->children() as $extension) {
$option = $extension->attributes('option');
if ($option == 'com_acesef') {
$latest_version = trim($extension->attributes('version'));
break;
}
}
break;
}
}
}

You can also append the &catid=6 to the XML link so that just the extensions of that category will be grabbed.

And this is the getRemoteData function which uses cURL, fsockopen, fopen and file_get_content PHP functions to grab remote data (just copy-paste it, zero-changes needed):


function getRemoteData($url) {
$useragent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.1.3) Gecko/20090824 Firefox/3.5.3 (.NET CLR 3.5.30729)";
$data = false;

// cURL
if (extension_loaded('curl')) {
// Init cURL
$ch = @curl_init();

// Set options
@curl_setopt($ch, CURLOPT_URL, $url);
@curl_setopt($ch, CURLOPT_HEADER, 0);
@curl_setopt($ch, CURLOPT_FAILONERROR, 1);
@curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
@curl_setopt($ch, CURLOPT_USERAGENT, $useragent);

// Set timeout
@curl_setopt($ch, CURLOPT_TIMEOUT, 5);

// Grab data
$data = @curl_exec($ch);

// Clean up
@curl_close($ch);

// Return data
if ($data !== false) {
return $data;
}
}

// fsockopen
if (function_exists('fsockopen')) {
$errno = 0;
$errstr = '';

$url_info = parse_url($url);
if($url_info['host'] == 'localhost') {
$url_info['host'] = '127.0.0.1';
}

// Set timeout
$fsock = @fsockopen($url_info['scheme'].'://'.$url_info['host'], 80, $errno, $errstr, 5);

if ($fsock) {
@fputs($fsock, 'GET '.$url_info['path'].(!empty($url_info['query']) ? '?'.$url_info['query'] : '').' HTTP/1.1'."\r\n");
@fputs($fsock, 'HOST: '.$url_info['host']."\r\n");
@fputs($fsock, "User-Agent: ".$useragent."\n");
@fputs($fsock, 'Connection: close'."\r\n\r\n");

// Set timeout
@stream_set_blocking($fsock, 1);
@stream_set_timeout($fsock, 5);

$data = '';
$passed_header = false;
while (!@feof($fsock)) {
if ($passed_header) {
$data .= @fread($fsock, 1024);
} else {
if (@fgets($fsock, 1024) == "\r\n")
$passed_header = true;
}
}

// Clean up
@fclose($fsock);

// Return data
if ($data !== false) {
return $data;
}
}
}

// fopen
if (function_exists('fopen') && ini_get('allow_url_fopen')) {
// Set timeout
if (ini_get('default_socket_timeout') < 5) {
ini_set('default_socket_timeout', 5);
}
@stream_set_blocking($handle, 1);
@stream_set_timeout($handle, 5);
@ini_set('user_agent',$useragent);

$url = str_replace('://localhost', '://127.0.0.1', $url);

$handle = @fopen ($url, 'r');

if ($handle) {
$data = '';
while (!feof($handle)) {
$data .= @fread($handle, 8192);
}

// Clean up
@fclose($handle);

// Return data
if ($data !== false) {
return $data;
}
}
}

// file_get_contents
if(function_exists('file_get_contents') && ini_get('allow_url_fopen')) {
$url = str_replace('://localhost', '://127.0.0.1', $url);
@ini_set('user_agent',$useragent);
$data = @file_get_contents($url);

// Return data
if ($data !== false) {
return $data;
}
}

return $data;
}



Share