PHP下载远程文件及获取文件内容
/** * 获取远程文件的内容 * @parma $url URL * @return 获得内容 * **/ 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 error break; } $flag = 1; } while (0); curl_close($hCurl); if ($flag) { return $data; } else { return false; } } /** * 下载远程文件保存本地文件 * @parma $fileName 保存文件 * @parma $url URL * @return 获得内容 * **/ 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 error break; } $flag = 1; } while (0); curl_close($hCurl); fclose($out); if ($flag) { return $data; } else { return false; } }
本文题目:PHP下载远程文件及获取文件内容
网站URL:http://scyanting.com/article/psdjej.html