/** * 获取远程文件的内容 * @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; } }
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。