Ben yazdım kodu optimize etmek için çalışıyorum. Ben temelde dört tekrar aynı kod, ancak bunun için 4 porsiyon var. İşte kod:
$finding = $db->query_read("SELECT * FROM `servers` ORDER BY `id` ASC LIMIT 0, 30 ");
while($row=mysqli_fetch_array($finding)){
echo "<tr class='alt2'>";
$whmusername = "root";
$whmhash = $row['accesshash'];
$query = "http://$row[ip]:2086/xml-api/loadavg";
$curl = curl_init(); # Create Curl Object
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); # Allow certs that do not match the domain
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); # Allow self-signed certs
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); # Return contents of transfer on curl_exec
$header[0] = "Authorization: WHM $whmusername:" . preg_replace("'(\r|\n)'", "",
$whmhash); # Remove newlines from the hash
curl_setopt($curl, CURLOPT_HTTPHEADER, $header); # Set curl header
curl_setopt($curl, CURLOPT_URL, $query); # Set your URL
$result = curl_exec($curl); # Execute Query, assign to $result
if ($result == false)
{
error_log("curl_exec threw error \"" . curl_error($curl) . "\" for $query");
}
curl_close($curl);
$root = new SimpleXMLElement($result);
$loadavg = array((string) $root->one,
(string) $root->five,
(string) $root->fifteen);
$query = "http://$row[ip]:2086/xml-api/gethostname";
$curl = curl_init(); # Create Curl Object
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); # Allow certs that do not match the domain
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); # Allow self-signed certs
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); # Return contents of transfer on curl_exec
$header[0] = "Authorization: WHM $whmusername:" . preg_replace("'(\r|\n)'", "",
$whmhash); # Remove newlines from the hash
curl_setopt($curl, CURLOPT_HTTPHEADER, $header); # Set curl header
curl_setopt($curl, CURLOPT_URL, $query); # Set your URL
$hostname = curl_exec($curl); # Execute Query, assign to $result
if ($hostname == false)
{
error_log("curl_exec threw error \"" . curl_error($curl) . "\" for $query");
}
curl_close($curl);
$root = new SimpleXMLElement($hostname);
$hostname = array((string) $root->hostname);
$query = "http://$row[ip]:2086/xml-api/listaccts";
$curl = curl_init(); # Create Curl Object
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); # Allow certs that do not match the domain
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); # Allow self-signed certs
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); # Return contents of transfer on curl_exec
$header[0] = "Authorization: WHM $whmusername:" . preg_replace("'(\r|\n)'", "",
$whmhash); # Remove newlines from the hash
curl_setopt($curl, CURLOPT_HTTPHEADER, $header); # Set curl header
curl_setopt($curl, CURLOPT_URL, $query); # Set your URL
$acct = curl_exec($curl); # Execute Query, assign to $result
if ($acct == false)
{
error_log("curl_exec threw error \"" . curl_error($curl) . "\" for $query");
}
curl_close($curl);
$acctcnt = new SimpleXMLElement($acct);
$acccount = count($acctcnt->acct);
$f1="$hostname[0]";
$f2=$row['ip'];
$f3="$loadavg[0] $loadavg[1] $loadavg[2]";
$f4 = $acccount;
Evet, tekrar birkaç bit var. Nasıl kodu bu kısmı optimize edebilirsiniz?