Does file_get_contents () function block?

I connect to an untrusted API through file_get_contents. Since it is unreliable, I decided to put the api call in the while loop:

$resultJSON = FALSE; while(!$resultJSON) { $resultJSON = file_get_contents($apiURL); set_time_limit(10); } 

Putting it another way: say that the API fails twice before making the third attempt. Did I send 3 requests, or did I send as many hundreds of requests as fit in this 3 second window?

+4
source share
4 answers

file_get_contents() , like all functions in PHP, is a blocking call.

+7
source

Yes, this is a lock feature. You should also check to see if the value is false. (Note that === is used, not ==.) Finally, you want to sleep for 10 seconds. set_time_limit () is used to set the maximum execution time before it is automatically destroyed.

 set_time_limit(300); //Run for up to 5 minutes. $resultJSON = false; while($resultJSON === false) { $resultJSON = file_get_contents($apiURL); sleep(10); } 
+2
source

Extension of @Sammitch's suggestion for using cURL instead of file_get_contents() :

 <?php $apiURL = 'http://stackoverflow.com/'; $curlh = curl_init($apiURL); // Use === not == // if ($curlh === FALSE) handle error; curl_setopt($curlh, CURLOPT_FOLLOWLOCATION, TRUE); // maybe, up to you curl_setopt($curlh, CURLOPT_HEADER, FALSE); // or TRUE, according to your needs curl_setopt($curlh, CURLOPT_RETURNTRANSFER, TRUE); // set your timeout in seconds here curl_setopt($curlh, CURLOPT_CONNECTTIMEOUT, 30); curl_setopt($curlh, CURLOPT_TIMEOUT, 30); $resultJSON = curl_exec($curlh); curl_close($curlh); // if ($resultJSON === FALSE) handle error; echo "$resultJSON\n"; // Now process $resultJSON ?> 

There are many more curl_setopt options. You must check them out.

Of course, this assumes you have cURL.

+1
source

I am not aware of any function in PHP that does not “block”. Alternatively, and if your server allows such things, you can:

  • Use pcntl_fork() and do other things in the script, waiting for the API call to complete.
  • Use exec() to call another script in the background [using & ] to call the API for you if pcntl_fork() not available.

However, if you literally can’t do anything in your script without successfully invoking this API, then it really doesn’t matter whether the call is “blocks” or not. What you really need to worry about is taking so long, expecting this API to exceed the configured max_execution_time , and your script will be interrupted in the middle without proper completion.

 $max_calls = 5; for( $i=1; $i<=$max_calls; $i++ ) { $resultJSON = file_get_contents($apiURL); if( $resultJSON !== false ) { break; } else if( $i = $max_calls ) { throw new Exception("Could not reach API within $max_calls requests."); } usleep(250000); //wait 250ms between attempts } 

It is worth noting that file_get_contents() has a default timeout of 60 seconds , so you are really threatened by the script being killed. Pay close attention to using cURL as you can set much more reasonable timeout values.

+1
source

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


All Articles