摘要:本文實(shí)例講述了PHP實(shí)現(xiàn)帶重試功能的curl連接方法。分享給大家供大家參考,具體如下:/** * @param string $url 訪問(wèn)鏈接 * @param string $target 需要重試的標(biāo)準(zhǔn): 返回結(jié)果中是否包含$target字符串 *&nbs
本文實(shí)例講述了PHP實(shí)現(xiàn)帶重試功能的curl連接方法。分享給大家供大家參考,具體如下:
/** * @param string $url 訪問(wèn)鏈接 * @param string $target 需要重試的標(biāo)準(zhǔn): 返回結(jié)果中是否包含$target字符串 * @param int $retry 重試次數(shù), 默認(rèn)3次 * @param int $sleep 重試間隔時(shí)間, 默認(rèn)1s * @return bool|mixed curl返回結(jié)果 * 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); // 信任任何證書(shū) curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 1); // 檢查證書(shū)中是否設(shè)置域名(為0也可以,就是連域名存在與否都不驗(yàn)證了) $output = curl_exec($ch); while((strpos($jsonOutput, $target) === FALSE) && $retry--){ //檢查$targe是否存在 sleep($sleep); //阻塞1s $output = curl_exec($ch); } curl_close($ch); return $output; }
更多關(guān)于PHP實(shí)現(xiàn)帶重試功能的curl連接示例請(qǐng)關(guān)注PHP中文網(wǎng)(www.miracleart.cn)其它文章!