curl_init回来的编码怎么判断

发布时间:2017-03-30 11:12:21编辑:丝画阁阅读(385)

<?php
$url = "http://www.homeforexchange.cn";
 
echo fopen_url($url);
function fopen_url($url) 
    if (function_exists('curl_init'))
    { 
        $curl_handle = curl_init(); 
        curl_setopt($curl_handle, CURLOPT_URL, $url); 
        curl_setopt($curl_handle, CURLOPT_CONNECTTIMEOUT,2); 
        curl_setopt($curl_handle, CURLOPT_RETURNTRANSFER,1); 
        curl_setopt($curl_handle, CURLOPT_FAILONERROR,1); 
        curl_setopt($curl_handle, CURLOPT_TIMEOUT,2);
        $file_content = curl_exec($curl_handle);
        $encode = mb_detect_encoding($file_content, array("ASCII","UTF-8","GB2312","GBK","BIG5"));       
        if($encode != "UTF-8")
        {
           $file_content = mb_convert_encoding($file_content, "UTF-8", $encode);
           //$file_content = iconv($encode,'utf-8//IGNORE',$file_content);
        }
        curl_close($curl_handle); 
    }
    else
    { 
        $file_content = ''; 
    } 
    return $file_content; 
}

?>


返回的数据中有:
<meta http-equiv="content-type" content="text/html;charset=gbk" />
根据他就可知道页面编码

没有时才需要编程判断
mb_detect_encoding 判断常有失误,所以又增加了 mb_check_encoding 函数



关键字