0

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 ONCE.

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;
}
iCeR
  • 77
  • 1
  • 7
  • If you're hinting at persistent http connections, you'll have to stick with what you already have. `cURL` is not designed to work this way. – Linus Kleen Dec 19 '10 at 15:44
  • @goreSplatter - would it be posible to loop $tmp = curl_exec ($ch); making $tmp an array, ie: $tmp(i) and incrementing i every loop? on each loop, output.. would this execute the availability check and output or is it impossible to skip executing curl for every single availability check? – iCeR Dec 19 '10 at 15:48
  • Is there another way of using http-post to achieve what I want? – iCeR Dec 19 '10 at 15:49
  • I think it's possible to wrap `GetCurlPage` in a loop. I still don't see the point in rapidly executing one and the same HTTP request in a loop... – Linus Kleen Dec 19 '10 at 15:53
  • possible duplicate of [Persistent/keepalive HTTP with the PHP Curl library?](http://stackoverflow.com/questions/972925/persistent-keepalive-http-with-the-php-curl-library) – Marc B Dec 19 '10 at 15:54
  • But then it is initializing and executing from scratch on each iteration? What I am trying to do is run these checks as quickly as possible.. as the output will be for example "not available" and then become "available" at one point. – iCeR Dec 19 '10 at 15:55

1 Answers1

0

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).

Community
  • 1
  • 1
Marc B
  • 356,200
  • 43
  • 426
  • 500
  • @Marc B - thanks! Excuse my lack of cURL knowledge, but how can I implement that with the above code whilst setting a loop of checks? Sorry for the nuisance. – iCeR Dec 19 '10 at 15:58
  • If you let the curl handle go out of scope, it'll be cleaned up by PHP. Create the curl handle OUTSIDE the function and pass it in as a parameter, or store it in a static variable inside the function. Either way, don't create a new handle each time. Just reuse it on the 2nd and subsequent requests. All you'd have to do is change the URL each time. – Marc B Dec 19 '10 at 16:00
  • Ok what I understood was to create this "curl_setopt($curlHandle, CURLOPT_HTTPHEADER, array( 'Connection: Keep-Alive', 'Keep-Alive: 300' )); " outside of the GetCurlPage function.. the rest was kind of confusing. Do you have a quick moment spare to markup my code with comments so I can insert the required code? Sorry again.. – iCeR Dec 19 '10 at 16:05
  • Giving this a shot right now.. thank you!! Will post back in a few. – iCeR Dec 19 '10 at 16:57
  • updated question with code at bottom.. kindly amend if necessary – iCeR Dec 19 '10 at 17:07
  • All the updated code would be doing is running curl on the same url 5 times with essentially no pause between them. Then returns the results of the last run. Unless the page only does something on the 5th repeated hit, then your code is doing 4 useless curl runs. Otherwise, looks ok – Marc B Dec 19 '10 at 17:22
  • Ok great! it's running fine, just slow still :( 500ms between checks. Can I speed it up to less than 100ms? – iCeR Dec 19 '10 at 17:34
  • 03:39:45:160632, 03:39:45:793075, 03:39:46:367587, 03:39:46:824030, 03:39:47:693148 – iCeR Dec 19 '10 at 17:40
  • The only thing you have control over is the php code, which is mininal. Most of that 500ms will be curl doing its thing, which is network/server lag, over which you have no control – Marc B Dec 19 '10 at 18:49
  • Not a problem - as this is a http-post, rather than using curl, is there another way to post the url (check domain availability) in a loop, and output, without that lag? – iCeR Dec 20 '10 at 03:00
  • Like I said, most of the lag will be curl doing its thing. network overhead, however long it takes the remote server to answer, etc... Time spent in your code, which is the only thing you can optimize, is minimal. Especially for such a small chunk. Perhaps you could issue the requests in parallel with a `curl_multi` call, but each request'd still take the same amount of time. – Marc B Dec 20 '10 at 06:06
  • Thanks! Ok so curl will lag no matter what. Can I post that url: https://www.apisite.com.au/availability/check.php?domain=testdomain&suffixes=.com.au another way, in a loop, without curl, so it will do the checks? – iCeR Dec 20 '10 at 06:12
  • There's plenty of other ways of doing a POST, but all you're doing is changing things on your end. The major lag is the remote server, and you have absolutely no control over that whatsoever. Doesn't matter how fast your code is, it's the same service that's taking its own sweet time to answer. – Marc B Dec 20 '10 at 06:19
  • I completely agree. Just trying to avoid using curl ;) Any example links of doing a POST in my application and avoiding curl? No more questions after that :) thank you!! – iCeR Dec 20 '10 at 06:25
  • Thanks Marc B. Works well in terms of speed - just little fix ups required as posted here. http://stackoverflow.com/questions/4487570/stream-context-create-not-showing-correct-ouput. You're welcome to take a look. This questions was just getting too long :) Thanks again!! – iCeR Dec 20 '10 at 07:08
  • The other question's code looks ok. But you really don't get the speed thing. Try an analogy: You need to pick up 5 things from the store. You train for decades and become the world's fastest sprinter, and can complete the run to/from the store in under 1 seconds. But the cashier still takes a minute to process your purchase. So each purchase is 1min1sec long. Even if you reach the speed of light and get there in 0 seconds, it'll still take a minute to buy the item. You cannot optimize your speed any more, because the major portion of the time is now spent in a line inside the store. – Marc B Dec 20 '10 at 14:01