web-dev-qa-db-fra.com

Wordpress Version vérifier avec PHP

J'ai créé un petit script qui vérifie la dernière version wordpress d'un site. Pour certains, c'est correct si le site Web affiche les informations relatives à la version, mais il y a un problème: s'il y a plus de nom = "générateur", il affiche la dernière ligne. .

Des idées les gars? Merci.

<?php 

    function file_get_contents_curl($url)
    {
        $ch = curl_init();

        curl_setopt($ch, CURLOPT_HEADER, 0);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_URL, $url);
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

        $data = curl_exec($ch);
        curl_close($ch);

        return $data;
    }

    $link = mysqli_connect("localhost", "", "", "");    

    if($link === false) { die('No connection!' . mysqli_connect_error()); }

    $sql = "SELECT * FROM My_Websites ORDER BY date";

    if($result = mysqli_query($link, $sql)) {

        if(mysqli_num_rows($result) > 0) {

            echo '<table class="table table-striped table-hover">';        
            echo '<thead>'; 
            echo "<tr>";
            echo "<th>ID</th>";
            echo "<th>Adaugat</th>";
            echo "<th>Domeniu</th>";
            echo "<th>Versiune WP.</th>";
            echo "<th>Admin</th>";
            echo "</tr>";
            echo "</thead>";

            while($row = mysqli_fetch_array($result)){   

                $domeniu = $row['website'];
                $html = file_get_contents_curl($domeniu);

                //parsing begins here:
                $doc = new DOMDocument();
                @$doc->loadHTML($html);
                $nodes = $doc->getElementsByTagName('title');

                //get and display what you need:
                $title = $nodes->item(0)->nodeValue;

                $metas = $doc->getElementsByTagName('meta');

                for ($i = 0; $i < $metas->length; $i++)
                {
                    $meta = $metas->item($i);
                    if($meta->getAttribute('name') == 'description')
                        $description = $meta->getAttribute('content');
                    if($meta->getAttribute('name') == 'generator')
                        $generator = $meta->getAttribute('content');
                }

                echo "<tr>";
                echo "<td> " . $row['id'] . " </td>";
                echo "<td> " . $row['date'] . " </td>";
                echo '<td> <a href="http://' . $domeniu . '" target="_blank">' . $domeniu . '</a></td>';
                echo "<td> " . $generator . " </td>";
                echo '<td> <a href="http://' . $domeniu . '/wp-admin" type="button" class="btn btn-default btn-sm"> <span class="glyphicon glyphicon-link" style="color:blue;"></span> acceseaza </a></td>';
                echo "</tr>";

            }     

            echo "</table>";      
            mysqli_free_result($result);    

        } else { print ('Error!'); }    

    } else { print 'Error!' . mysqli_error($link); print '.'; }

    mysqli_close($link);

?>
1
Alex Grecu

Essayez de vérifier le contenu de l'attribut avant de définir la valeur:

$generator = ''; // clear previous value
for ($i = 0; $i < $metas->length; $i++)
    {
        $meta = $metas->item($i);
        if($meta->getAttribute('name') == 'description')
            $description = $meta->getAttribute('content');

        if($meta->getAttribute('name') == 'generator') {
            $thisgenerator = $meta->getAttribute('content');
            if ( ($generator == '') && (stristr($thisgenerator,'wordpress')) ) {
                $generator = $thisgenerator;
            }
        }
    }
1
majick