How to properly handle cURL timeouts and implement retries in PHP when making API requests

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!

Been a long time since i did anything with cURL in PHP, but sniffing curl_errno should be the answer?