Home >Backend Development >PHP Tutorial >PHP implements curl connection example with retry function
This article mainly introduces the curl connection method with retry function in PHP, and analyzes the method of PHP using curl to achieve repeated connections in the form of examples. Friends in need can refer to it
The details are as follows:
/** * @param string $url 访问链接 * @param string $target 需要重试的标准: 返回结果中是否包含$target字符串 * @param int $retry 重试次数, 默认3次 * @param int $sleep 重试间隔时间, 默认1s * @return bool|mixed curl返回结果 * desc 有重试功能的curlget */ function curlGetRetry($url, $target, $retry=3, $sleep = 1) { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); curl_setopt($ch, CURLOPT_TIMEOUT, 5); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // 信任任何证书 curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1); // 检查证书中是否设置域名(为0也可以,就是连域名存在与否都不验证了) $output = curl_exec($ch); while((strpos($jsonOutput, $target) === FALSE) && $retry--){ //检查$targe是否存在 sleep($sleep); //阻塞1s $output = curl_exec($ch); } curl_close($ch); return $output; }
Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.
Related recommendations:
phpInterface technology examples and detailed explanations with pictures and texts
phpDetailed explanation of abstract methods and abstract class instances
Usage of static and const keywords in php
The above is the detailed content of PHP implements curl connection example with retry function. For more information, please follow other related articles on the PHP Chinese website!