/** * get the contents of a remote file * @parma $url URL * @return Get content * **/ public static function getcontent ($url) {$flag = 0;do {$hCurl = curl_init (); curl_setopt ($hCurl, curlopt_header, 0); curl_setopt ($hCurl, curlopt_ returntransfer, 1); curl_setopt ($hCurl, curlopt_timeout, 120); curl_setopt ($hCurl, CURLOPT_ connecttimeout, 4) curl_setopt ($hCurl, curlopt_url, $url); $data = curl_exec ($hCurl); if (Curl_errno ($hCurl)) {break;} $http _code = curl_getinfo ($hCurl, curlinfo_http_code);if ($http _code >= 400) { //400 - 600 is server errorbreak;} $flag = 1;} while (0); Curl_close ($hCurl);if ($flag) {return $data;} else {return&nBsp;false;}} /** * download remote File save local file * @parma $fileName Save file * @parma $url URL * @return Get content * **/public static Function getcontenttofile ($fileName, $url) {$out = fopen ($fileName, ' W ');if ( Empty ($out)) {return false;} $flag = 0;do {$hCurl = curl_init (); curl_setopt ($hCurl, curlopt_header, 0); curl_setopt ($hCurl, curlopt_returntransfer, 1); curl_setopt ($hCurl, curlopt_timeout, 120); curl_setopt ($hCurl, curlopt_connecttimeout, 4); curl_setopt ($hCurl, curlopt_file, $out); curl_setopt ($hCurl, curlopt_url, $url), $data = curl_exec ($hCurl);if (Curl_errno ($ Hcurl)) {break;} $http _code = curl_getinfo ($hCurl, curlinfo_http_code);if ($http _code >= 400) { //400 - 600 is server errorbreak;} $flag = 1;} while (0); Curl_close ($hCurl); fclose ($out);if ($flag) {return $data;} else {return false;}}
This article is from the "dream to think XI" blog, please be sure to keep this source http://qiangmzsx.blog.51cto.com/2052549/1618903
PHP download remote files and get file contents