I’m using cURL in PHP to fetch data from an external API. Sometimes the request times out or fails due to a temporary network issue. I want to set a timeout and also retry the request a few times before giving up.
What’s the best way to implement this in a clean and reliable way?
Should I use CURLOPT_TIMEOUT
or CURLOPT_CONNECTTIMEOUT
, or both?
Also, how can I properly loop the retries while avoiding infinite loops or bad practices?
Sample code would be really helpful!