php如何增强file_get_contents函数的兼容性相关介绍

巴扎黑
풀어 주다: 2023-03-14 20:50:02
원래의
1529명이 탐색했습니다.


php增强file_get_contents的兼容性, 优先选择CURL拓展

function rlib_file_get_contents($url, $referer = null, $timeout = 10){
	static $curl_enabled = -1;
	if ($curl_enabled == -1){
		$curl_enabled = (extension_loaded('curl') && function_exists('curl_exec')) ? 1 : 0;
	}
	$contents = null;
	if ($curl_enabled == 1){
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL, $url);
		curl_setopt($ch, CURLOPT_TIMEOUT, $timeout);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
		curl_setopt($ch, CURLOPT_REFERER, ($referer == null ? $url : $referer));
		curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER["HTTP_USER_AGENT"]);
		//curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept-Encoding: gzip, deflate'));
	    	//curl_setopt($ch, CURLOPT_ENCODING, 'gzip,deflate');
		curl_setopt($ch, CURLOPT_HTTPHEADER, array('Expect:'));
		$contents = curl_exec($ch);	
		if ($contents == FALSE){
			global $g_lastError;
			$g_lastError = curl_error($ch);
			$contents = null;
		}
		curl_close($ch);
	} else {
		$contents = file_get_contents($url, false, stream_context_create(array(
			'http' => array(
				'timeout' => $timeout,
				'header' => 'Referer: ' . ($referer == null ? $url : $referer) . '\r\n' .
							'User-Agent: ' . $_SERVER["HTTP_USER_AGENT"] . '\r\n'
			)
		)));
		if ($contents == FALSE){
			global $g_lastError;
			$g_lastError = 'file_get_contents出错';
			$contents = null;
		} else {
			//$contents = mb_convert_encoding($contents, 'UTF-8', mb_detect_encoding($contents, 'UTF-8, GBK, GB2312', true));
		}
	}
	return $contents;
}
로그인 후 복사


위 내용은 php如何增强file_get_contents函数的兼容性相关介绍의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!