1

I'm trying to optimize the code that I've written. I've basically got 4 portions to it, however, all four repeat the same code. Here's the code:

$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;

Yeah, there's a few bits of repetition. How can I optimize this portion of code?

3 Answers 3

2

I would recommend using functions to separate your code, That way everything would be easier to follow. If you are comfortable you can use object oriented methodology to put it into classes. I would also use an opcode cache (like APC) to save it recompiling the code every time.

Hope that helps, RayQuang

Sign up to request clarification or add additional context in comments.

1 Comment

APC is installed. cURL is the rate determining step in the code, and that can be optimised.
1

You'd probably see the greatest time benefit from running your cURL requests concurrently instead of serially. Look into using the curl_multi_* functions which will allow you to run the requests in parallel.

2 Comments

I can't seem to find great example of how to implement this. Can you help?
Check somacon.com/p537.php it is a simple (as possible, given the nature of curl_multi*) example.
1

This might not be a useful suggestion, but I'll add it anyhow. My advise would be to drop cURL and try to use the HTTP extension. It has a way less arcane and overly parameter-procedural API than the cURL library (which is actually a multiprotocol tool, and just does HTTP as a side job.)

It's not preinstalled on all shared hosting servers, but worth asking for. In particular it provides a simpler HttpRequestPool API for issuing multiple requests. And for once it's easier to reuse code if you extend the base classes, but writing wrapper functions (which would be wise in your case) is also easier with it (reusing instantiated request objects).

As backend it incidently uses cURL, but at least you're freed from tuning parameters to sensible defaults. There's not much you can do to speed up processing and request time. But there's no difference there between HTTP libraries in PHP anyway (you don't have to use compiled extensions for speed). So you could easily also use PEAR HTTP_Request2 or Zend_HTTP, which also provide much much much more legible APIs.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.