我正在尝试优化我编写的代码。我基本上有4个部分,但是,所有四个重复相同的代码。这是代码:
$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;
是的,有几点重复。如何优化这部分代码?
答案 0 :(得分:2)
我建议使用函数来分隔代码,这样一切都会更容易理解。如果您感到舒服,可以使用面向对象的方法将其放入类中。我还会使用操作码缓存(如APC)来保存每次重新编译代码。
希望有所帮助, RayQuang
答案 1 :(得分:1)
您可能会看到同时运行cURL请求而不是串行运行的最佳时间。查看使用curl_multi_*
函数,它们允许您并行运行请求。
答案 2 :(得分:1)
这可能不是一个有用的建议,但无论如何我会添加它。我的建议是放弃cURL并尝试使用HTTP extension。与cURL库(实际上是一个多协议工具,只是将HTTP作为副作业)相比,它有一种不那么神秘且过度参数化的API。
它并未预装在所有共享主机服务器上,但值得一提。特别是它提供了一个更简单的HttpRequestPool
API来发出多个请求。而且,如果扩展基类,重用代码会更容易,但编写包装函数(在您的情况下这将是明智的)也更容易(重用实例化的请求对象)。
作为后端,它无意中使用了cURL,但至少你没有将参数调整为合理的默认值。你没有太多办法可以加快处理速度和请求时间。但无论如何,PHP中的HTTP库之间没有区别(您不必使用编译的扩展来提高速度)。因此,您可以轻松地使用PEAR HTTP_Request2或Zend_HTTP,它们也提供了更加清晰的API。