Ec (2); & nbsp; copy the code function_link ($ url) {& nbsp; $ contents @ file_get_contents (& quot; $ url & quot;); & nbsp; if ($ contents & quot; Forbidden & quot; | $ contents & quot;) {& nbs script ec (2); script
The Code is as follows: |
|
Function _ link ($ url ){ $ Contents = @ file_get_contents ("$ url "); If ($ contents = "Forbidden" | $ contents = ""){ $ Ch = curl_init (); $ Timeout = 5; Curl_setopt ($ ch, CURLOPT_URL, "$ url "); Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, 1 ); Curl_setopt ($ ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1; SV1 )"); Curl_setopt ($ ch, CURLOPT_CONNECTTIMEOUT, $ timeout ); $ Contents = curl_exec ($ ch ); Curl_close ($ ch ); } If (empty ($ contents )){ Exit ('cant locaion .'); } Preg_match_all ("/charset = (. *?)> /Is ", $ contents, $ cod ); If (! Empty ($ cod [1] [0]) { If (preg_match ("/UTF-8/I", $ cod [1] [0]) { $ Contents = iconv ("UTF-8", "gbk // Transcoder", $ contents ); } } Return $ contents; } |