web-dev-qa-db-ja.com

Wordpressのバージョンチェック PHP

ウェブサイトにバージョンに関する情報が表示されている場合は問題ありませんが、name = "generator"がある場合は最後の行になるという問題があります。

何かアイデアはありますか?ありがとう。

<?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

値を設定する前に属性の内容を確認してください。

$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