PHP implementation with Retry Curl Connection Example _php technique

Source: Internet
Author: User
Tags curl php programming php regular expression sleep

This article describes the PHP implementation with retry function Curl connection method. Share to everyone for your reference, specific as follows:

/**
 * @param string  $url Access Link
 * @param string $target criteria to retry: Returns whether the result contains a $target string
 * @param int $retry retry times Number, default 3 times
 * @param int $sleep retry interval, default 1s
 * @return bool|mixed Curl return results
 * desc have retry curlget * *
Functi On 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); Trust any certificate
  curl_setopt ($ch, Curlopt_ssl_verifyhost, 1);//Check the certificate to set the domain name (0 is also OK, even if the domain name exists or not verified)
  $output = Curl_ EXEC ($ch);
  while (Strpos ($jsonOutput, $target) = = FALSE) && $retry-) {//check $targe for Sleep
    ($sleep);//block 1s
    $ Output = Curl_exec ($ch);
  }
  Curl_close ($ch);
  return $output;
}

More about PHP Interested readers can view the site topics: "Php Curl Usage Summary", "PHP array" operation Skills Daquan, "PHP Sorting algorithm Summary", "PHP common traversal algorithm and skills summary", "PHP Data structure and algorithm tutorial", " PHP Programming algorithm Summary, "PHP Mathematical Calculation Skills Summary", "PHP Regular Expression Usage summary", "PHP operation and operator Usage Summary", "PHP string (String) Usage summary" and "PHP common database Operation skill Summary"

I hope this article will help you with the PHP program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.