最新赞助活动温馨提示:自愿赞助服务器费用,学生和没有工作的整站资源免费下载!
头像

php怎么样增强file_get_contents函数的兼容性相关介绍

来源:http://erdangjiade.com/topic/1164.html 你好,世界。 2017-09-25 22:42浏览(14)

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) . '
' .
							'User-Agent: ' . $_SERVER["HTTP_USER_AGENT"] . '
'
			)
		)));
		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函数的兼容性相关介绍的详细内容,更多请关注二当家的素材网其它相关文章!

评论0
头像

友情提示:垃圾评论一律封号 加我微信:826096331拉你进VIP群学习群

1 2