0

I am connecting up to an API and as a result it should loop and output 5 iterations of the results from the checks.

The code

<?php

      $i = 0;

$opts = array(
  'https'=>array(
    'method'=>"POST",
    'header'=>"Accept-language: en\r\n" .
              "Cookie: foo=bar\r\n"
  )
);

$context = stream_context_create($opts);

$fp = fopen('https://ssl.theapidomain.com.au/check.php?domain=testdomain&suffixes=com.au', 'r', false, $context);

    while ($i < 5) {
      fpassthru($fp);
      $out = explode('<br>', $fp);
      echo $out[0];
      echo "<br>";
      echo $out[1];
      echo "<br>";
      echo date('H:i:s');
      echo "<br>";
      $i++;
    }

fclose($fp);

?>

The output

available: testdomain.com.au not available: whoisfailure: Resource id #2

16:58:57

Resource id #2

16:58:57

Resource id #2

16:58:57

Resource id #2

16:58:57

Resource id #2

16:58:57

It should be outputting this 5 times:

available: testdomain.com.au not available: 16:58:57

It seems when I echo $out[0] and [2], it displays the resource id rather than the information inside (available / not available).

hakre
  • 193,403
  • 52
  • 435
  • 836
iCeR
  • 77
  • 1
  • 7

1 Answers1

0

You must use fread() on that filepointer $fp, not fpassthru(). You'll get a string result from fread which you can then feed into explode(). Otherwise explode just reads the $fp which outputs nothing but Resource #123.

 $html = fread($fp, 16384);
 $out = explode("<br>", $html);

Btw, you should stop trying to microoptimize your domain query thing. The bottleneck is the network traffic, not making the loop run faster. The PHP stream wrapper isn't faster than cURL.

Community
  • 1
  • 1
mario
  • 144,265
  • 20
  • 237
  • 291
  • @mario thanks. Used the code from your example and it now outputs the first iteration properly, the subsequent ones only output the time. The thing is, i've had this running queries on 1/45th of a ms, using $domainslist, though it's limited to 20 in the array. So I'm trying to loop just one domain to do the checks. – iCeR Dec 20 '10 at 07:36
  • @iCeR: It reads just once. It does not pull any new data. It's no different than your previous code. – mario Dec 20 '10 at 08:10
  • So I basically need to include "$fp = fopen..." in the loop? (this is what 'wastes' time) hehe but necessary. – iCeR Dec 20 '10 at 08:24
  • @iCeR: Yes. Would you care to explain how **you** think HTTP requests work? – mario Dec 20 '10 at 08:28
  • @mario - executing a command to send information/data to the server via POST and optionally retrieving via the GET method? I know how it works, I'm just frustrated that curl and the like take so long between queries. I really want to minimise the time between queries. – iCeR Dec 20 '10 at 08:45
  • @mario - if I load up an array of domains in $domainslist, it will do the checks within the one curl connection. Limit of 20 (whether it be the same or different url's to check). If the API allows for multiple checking on the one curl connection/execution, is there a way I can do this through php, where in the one execution of curl it runs an array of availability checks? – iCeR Dec 20 '10 at 09:05