开发者

Help fixing php/api/curl code please

What I have in place, is a domain availability check, which connects up to an API and outputs "Available: and Unavailable:" from $tmp. Ths below code will only check the availability ONC开发者_运维技巧E.

I would like to check the availability of the domain, multiple times (possibly on a loop?), without having to run restart cURL connection everytime (as it wastes time - 300ms to 1s per query).

I just don't know how I can connect to cURL once and run the loop (doing the check through the API). Help adjusting the code would be very much appreciated! Minimizing the time it takes to output "available/not available" and looping the checks is key.

Thank you.

Current code

<?php

    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);
      echo $tmp[0];
      echo "<br>";
      echo $tmp[1];
      echo "<br>";
      return $tmp;
    }

$returnUrl = "http://www.mysite.com.au/check.php";
$url = "https://www.apisite.com.au/availability/check.php?domain=testdomain&suffixes=.com.au";
$output = GetCurlPage("$url");

?>

@Marc B

    function getCurlPage($pageSpec) {
if (is_null($ch)) {
    $ch = curl_init($pageSpec);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
} else {
    curl_setopt($ch, CURLOPT_URL, $pageSpec);
}
  while ($i < 5) {
  $tmp = curl_exec ($ch);
  //curl_close ($ch);
  $tmp = preg_replace('/(?s)<meta http-equiv="Expires"[^>]*>/i', '', $tmp);
  $tmp = explode('<br>', $tmp);
  echo $tmp[0];
  echo "<br>";
  echo $tmp[1];
  echo "<br>";
  echo udate('H:i:s:u');
  echo "<br><br>";
  $i++;
  }
      return $tmp;
}


This should answer your question: Persistent/keepalive HTTP with the PHP Curl library?

comment followup:

function getCurlPage($pageSpec) {
    if (is_null($ch)) 
        static $ch = curl_init($pageSpec);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
    } else {
        curl_setopt($ch, CURLOPT_URL, $pageSpec);
    }
    $tmp = curl_exec($ch);
    ... do NOT close the curl handle, otherwise do the rest the same as before ...
}

Probably won't work as is, doing this off the top of my head and with only 2 hours sleep, but this should be enough to get you started.

And by the way, there's no need to do doublequotes for GetCurlPage("$url"), it's a waste of parser time, as PHP will have to create a new empty string, stuff $url into it, and pass the new string on down. Just do GetCurlPage($url).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜