0

How can i force curl to stop reading from my endpoint if the request is taking longer than 200 milliseconds, For example i am using the below curl get request.

$curl = curl_init('http://example.com');
curl_setopt($curl, CURLOPT_FAILONERROR, true);
curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);  
$result = curl_exec($curl);
echo $result; 

I know this is possible using fsockopen and feof but i do not believe i can use fsockopen/feof because i bind my curl request to a specific interface / ip address using CURLOPT_INTERFACE

If this is not possible with curl, could someone please suggest an alternative solution for PHP where i can bind to the interface/ip and stop reading from the endpoint if 200ms is exceeded.

  • have you tried this flag `CURLOPT_TIMEOUT_MS`? maybe related https://stackoverflow.com/a/20462702/3859027 – Kevin May 20 '20 at 03:49
  • No i've not so that might be a viable option, would that exit the entire request if 200ms is exceeded? – LinuxNovice May 20 '20 at 03:54
  • i have not tried it myself, but the flag is applied on curl so the timeout applies to curl alone – Kevin May 20 '20 at 04:01
  • I have just tried it but it does not seem to work, i think it is because the connection is quick which is much less than 200ms but the delay which happens when reading from the endpoint so i assume thats why its being ignored / not making any difference. (Basically im trying to get my code to check if the endpoint is performing a task which takes longer than 200ms and if it is then dont bother reading the response and just move on) – LinuxNovice May 20 '20 at 04:06
  • if you have a test endpoint, make a sleeping endpoint to pseudo simulate it – Kevin May 20 '20 at 04:24

0 Answers0