1. determine whether a url can be accessed normally. avoid using file_get_contents because the url cannot be accessed, resulting in a fatal error and program termination. Program code $ url & amp; lsquo; http://www.2cto.com & amp; rsquo; $ chcurl_init (); $ timeout10; curl...
1. determine whether a url can be accessed normally. avoid using file_get_contents because the url cannot be accessed, resulting in a fatal error and program termination.
Program code
$ Url = 'http: // www.2cto.com ';
$ Ch = curl_init ();
$ Timeout = 10;
Curl_setopt ($ ch, CURLOPT_URL, $ url );
Curl_setopt ($ ch, CURLOPT_HEADER, 1 );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 );
Curl_setopt ($ ch, CURLOPT_CONNECTTIMEOUT, $ timeout );
$ Contents = curl_exec ($ ch );
If (false = $ contents)
{
Echo 'curl error: '. curl_error ($ ch );
}
Else
{
....
}
In addition, you can use
Program code
Curl_getinfo ($ ch, CURLINFO_HTTP_CODE );
Obtain the code returned by the HTTP header file. if it is 200, the url can be accessed normally. However, this function must be used after curl_exec (), which seems redundant.
From: follow the wind's BLOG