How can I rewrite my PHP script to run as quickly as possible?

I run a script that checks the availability (10 times) of the domain name and displays the domain, if available, and the timestamp (with milliseconds).

Can you find anything that slows down a script even magnate? If you could, please, tweak and republish or report what could be done better, that would be very appreciated! Thank.

<?php

    date_default_timezone_set('Australia/Brisbane');
    $loops = 0; 

    function udate($format, $utimestamp = null) {
      if (is_null($utimestamp))
        $utimestamp = microtime(true);

      $timestamp = floor($utimestamp);
      $milliseconds = round(($utimestamp - $timestamp) * 1000000);

      return date(preg_replace('`(?<!\\\\)u`', $milliseconds, $format), $timestamp);
    }

    function GetCurlPage ($pageSpec)
    {
      $ch = curl_init($pageSpec);
      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
      curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
      curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
      $tmp = curl_exec ($ch);
      curl_close ($ch);
      $tmp = preg_replace('/(?s)<meta http-equiv="Expires"[^>]*>/i', '', $tmp);
      $tmp = explode('<br>', $tmp);
      foreach ($tmp AS $line) {
        //echo '<pre>';
        //print_r($line);
        //echo '</pre>';
      }
      // Do something with each line.
      echo $tmp[0];
      echo "<br>";
      echo $tmp[1];
      //echo $tmp[2];
      echo "<br>";
      echo udate('H:i:s:u');
      echo "<br><br>";

      return $tmp;

    }

    while ($loops <= 10)    
    {
$suffixes=urlencode("com.au");
$domain = "sampledomain";
$fuzzysearch = "0";
$returnUrl="http://mydomain.com.au/test.php";
$url = "https://apidomain.com.au/check.php?domain=" .
$domain . "&suffixes=" . $suffixes . "&fuzzysearch=" . $fuzzysearch;
$output = GetCurlPage("$url");

    ++$loops;
    }           
?>
-1
source share
3 answers

Slow because you need to do 10 curls on an external site

Two suggestions

  • update test.php / check.php to allow checking multiple domain names with one call to curl (instead of checking one by one, pass an array)
  • curl_multi_exec, 10 URL-.

1

+5
  • $suffixes=urlencode("com.au"); $domain . "&suffixes=" . $suffixes . "&fuzzysearch=" . $fuzzysearch;
  • foreach ($tmp AS $line) {
  • udate , udate
+1

Change if (is_null($utimestamp))to if ($utimestamp === null)so that PHP does not call the function is_null().

0
source

Source: https://habr.com/ru/post/1782869/


All Articles