Home  >  Article  >  Backend Development  >  Does PHP CURL business need to set a timeout when calling third-party interfaces?

Does PHP CURL business need to set a timeout when calling third-party interfaces?

藏色散人
藏色散人forward
2021-03-10 15:23:332626browse

If you do not set the connection timeout and waiting timeout

php When using curl, you need to develop the habit of setting the timeout. Most of the online PHP Curl codes do not set it

  • Reptile business: Will block the process, especially when accessing foreign resources
  • Business interface: Affects user experience, it is necessary to set a timeout according to the business, and record error logs beyond the specified time Feedback from relevant personnel

Suggestions

1. Develop good habits
2. If you use PHP native CURL extension, it is best to encapsulate one yourself composer
3. When using a third-party library, such as guzzlehttp/guzzle, most authors will set a default time

PHP CURL timeout parameter

//连接超时时间
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); 
//等待响应超时时间二选一
curl_setopt($ch, CURLOPT_TIMEOUT, 10); //秒级
curl_setopt($ch, CURLOPT_TIMEOUT_MS, 10);//毫秒级

[Recommended learning: "PHP Video Tutorial"]

The above is the detailed content of Does PHP CURL business need to set a timeout when calling third-party interfaces?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:learnku.com. If there is any infringement, please contact admin@php.cn delete