PHP implementation method to detect whether the URL can be opened normally

Source: Internet
Author: User
This article mainly introduces the PHP detection URL can be opened normally, involving the simple use of curl in PHP, the need for friends to refer to the following

This is a test URL can open the normal PHP code, through the code below to detect whether a URL is normal access, if normal will return the value of the HTTP status code 200, if the other is not normal, this code we can use a lot of places, such as cached links to the ICO icon can be used, The cache detects if the site is normal, and if it is normal to cache the ICO icon, otherwise call a default icon file.

The code is as follows:

<?php/* * Created on 2016-9-4 * */function Httpcode ($url) {  $ch = Curl_init ();  $timeout = 3;  curl_setopt ($ch, curlopt_followlocation,1);  curl_setopt ($ch, curlopt_returntransfer,1);  curl_setopt ($ch, Curlopt_header, 1);  curl_setopt ($ch, Curlopt_connecttimeout, $timeout);  curl_setopt ($ch, Curlopt_url, $url);  Curl_exec ($ch);  return $httpcode = Curl_getinfo ($ch, curlinfo_http_code);  Curl_close ($ch);} echo "Judge Topic.alibabacloud.com's Link:". Httpcode (' http://www.php.cn ');? ><br/> If the display is 200 normal, if the display of other values is unhealthy; the next 3 of the $timeout is the set time-out number of seconds.

Operating effects such as:

Summary: The above is the entire content of this article, I hope to be able to help you learn.

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.