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

藏色散人
Release: 2023-04-09 19:50:01
forward
2574 people have browsed it

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);//毫秒级
Copy after login

[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!

Related labels:
source:learnku.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!