Get the latest IDS

-1

I want to get the latest ID's from this site

I do not want to get the IDS with higher numbers but the last ones in the order: Example:

<effect id="195" lib="CrossTrainer" type="fx" revision="62175"/>
<effect id="500" lib="BigSpn" type="fx" revision="90000"/>
<effect id="501" lib="SmallS" type="fx" revision="90000"/>
<effect id="502" lib="BigJmp" type="fx" revision="90000"/>
<effect id="503" lib="SmallJ" type="fx" revision="90000"/>
<effect id="504" lib="Hoverplan1" type="fx" revision="90000"/>
<effect id="505" lib="Hoverplan2" type="fx" revision="90000"/>
<effect id="506" lib="Hoverplan3" type="fx" revision="90000"/>
<effect id="507" lib="TrampolineTest" type="fx" revision="90000"/>

Get only the last 3 IDs ... "505,506,507"

    
asked by anonymous 24.06.2016 / 21:52

2 answers

1

Being an XML you can use DOMDocument

$url = 'http://hebbohotel.com.br/swf/gordon/RELEASE-HEBBO/effectmap.xml';

$xml = file_get_contents($url);

$dom = new DOMDocument;
$dom->loadXML($xml);
$effects = $dom->getElementsByTagName('effect');

foreach ($effects as $effect) {
    echo $effect->getAttribute('id'), PHP_EOL;
}

The previous example served only to understand, to get the last 3 ids use array (convert with iterator_to_array ) and use the function array_slice like this:

$url = 'http://hebbohotel.com.br/swf/gordon/RELEASE-HEBBO/effectmap.xml';

$xml = file_get_contents($url);

$dom = new DOMDocument;
$dom->loadXML($xml);
$effects = iterator_to_array($dom->getElementsByTagName('effect'));//Passa para array

//-3 no segundo parametro pega os 3 ultimos itens
$effects = array_slice($effects, -3);

foreach ($effects as $effect) {
    echo $effect->getAttribute('id'), PHP_EOL;
}

Note that file_get_contents fail you might be due to not enabling external urls access, you can enable this in php.ini (as I explained in this answer link ):

allow_url_fopen=1

Or you can change file_get_contents by curl, like this:

<?php
$url = 'http://hebbohotel.com.br/swf/gordon/RELEASE-HEBBO/effectmap.xml';

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); //Transferência binaria
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Força retornar transferência na variável

$xml = curl_exec($ch);

curl_close($ch);

$dom = new DOMDocument;
$dom->loadXML($xml);
$effects = iterator_to_array($dom->getElementsByTagName('effect'));//Passa para array

GZ compressed responses

Sometimes it can happen even if you do not request the header Accept-Encoding the server still send, this is a problem maybe of bad configuration, in case the link you want to access has this problem, I tried to send the headers, but apparently does not work, so one solution is to use gzdecode of php, like this:

  • file_get_contents

    $url = 'http://hebbohotel.com.br/swf/gordon/RELEASE-HEBBO/effectmap.xml';
    
    $xml = gzdecode(file_get_contents($url));
    
    $dom = new DOMDocument;
    $dom->loadXML($xml);
    $effects = iterator_to_array($dom->getElementsByTagName('effect'));//Passa para array
    
  • curl

    $url = 'http://hebbohotel.com.br/swf/gordon/RELEASE-HEBBO/effectmap.xml';
    
    $ch = curl_init();
    
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_BINARYTRANSFER, 1); //Transferência binaria
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //Força retornar transferência na variável
    
    $xml = gzdecode(curl_exec($ch));
    
    curl_close($ch);
    
    $dom = new DOMDocument;
    $dom->loadXML($xml);
    $effects = iterator_to_array($dom->getElementsByTagName('effect'));//Passa para array
    
25.06.2016 / 05:52
0

I do not know if it will help because you are working with XML , so, do not post if you are working with it as string or turning it into array .

I did not really use str_slipt , I did it differently.

I made a class to handle the message, GetIds.php :

<?php
class GetIds
{
    /*
    *   Atributos
    */
    private $string = null;
    private $limit = null;
    private $split = null;

    /*
    *   Construtor com insercao de um atributo
    */
    public function __construct($string = null)
    {
        $this->string = $string;
    }

    /*
    *   Varre atributo string até que o caracter lido não seja string, apos iss une os numeros formando o id
    *   @return result - array com os ids
    */
    public function getIds()
    {
        $this->appLimit();

        $result = array();
        foreach($this->split as $value)
        {
            $isNumber = 0;
            for($letter = 0; $letter < strlen($value); $letter ++)
            {

                if(filter_var($value[$letter], FILTER_VALIDATE_INT) or $value[$letter] === '0')
                    $isNumber ++;
                else
                    break;

            }
            for($letter = 0; $letter < count($value); $letter ++)
            {
                array_push($result, mb_substr($value, 0, $isNumber));
            }
        }

        return $result;
    }

    /*
    *   Muda o valor do atributo limit
    */
    public function setLimit($newLimit)
    {
        $this->limit = $newLimit;
    }

    /*
    *   Separa o atributo string em determinado separador passado por parametro
    */
    public function setSplit($separator)
    {
        if(!is_null($this->string))
            $this->split = explode('id="', $this->string);
        unset($this->split[0]);
    }

    /*
    *   Apaga elementos do array deixando os x ultimos
    */
    private function appLimit()
    {
        if(!is_null($this->string) and !is_null($this->limit))
        {
            $crop = count($this->split) - $this->limit;
            for($item = 0; $item < $crop; $item ++)
            {
                unset($this->split[$item]);
            }
        }

    }
}

test.php file :

<?php
require_once 'GetIds.php';
$myResult =
'<effect id="195" lib="CrossTrainer" type="fx" revision="62175"/>
<effect id="500" lib="BigSpn" type="fx" revision="90000"/>
<effect id="501" lib="SmallS" type="fx" revision="90000"/>
<effect id="502" lib="BigJmp" type="fx" revision="90000"/>
<effect id="503" lib="SmallJ" type="fx" revision="90000"/>
<effect id="5" lib="Hoverplan1" type="fx" revision="90000"/>
<effect id="122150" lib="Hoverplan2" type="fx" revision="90000"/>
<effect id="50654" lib="Hoverplan3" type="fx" revision="90000"/>
<effect id="502170" lib="TrampolineTest" type="fx" revision="90000"/>';

// cria uma classe com a string de resultado
$ids = new GetIds($myResult);

// define o limite para 3
$ids->setLimit(3);

// define onde devo cortar o resultado
$ids->setSplit('id="');

// pega os ids encontrados 
$result = $ids->getIds();

var_dump($result);

Output:

array (size=3)
  0 => string '122150' (length=6)
  1 => string '50654' (length=5)
  2 => string '502170' (length=6)

Obs: I changed the ids just to show that it works with id of different lengths.

    
25.06.2016 / 05:21