Introduction to how PHP enhances the compatibility of file_get_contents functions

Source: Internet
Author: User
PHP Enhanced file_get_contents compatibility, priority Choice Curl expansion

function rlib_file_get_contents ($url, $referer = null, $timeout = ten) {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"]. The IF ($contents = = FALSE) {global $g _lasterror; $g _lasterror = ' file_get_contents error '; $contents = null;} else {//$ Contents = mb_convert_encoding ($contents, ' UTF-8 ', mb_detect_encoding ($contents, ' UTF-8, GBK, GB2312 ', true));}} return $contents;}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.