Curl_init back to the code how to judge? Individual text garbled solution!
The code is as follows:
$url = "Http://zhidao.baidu.com/link?url= Ptwcjotq02pjg-mjcnc-fkw8onoy9x8q0esrcfhdvjy47agzndncb-bcatngrgdt9yi0tvless_w0apj8vsk0atvkvhnydzadn0kv0bznau ";
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;
}
?>
Individual characters are garbled! Strange, please look at the picture
What is the cause of this?
The above code seems to be wrong ah, the original page is clearly GB2312 lack of judgment out is CP936, no words AH
Please help to see if the above code needs to be perfected.
Thank you so much!
------to solve the idea----------------------
The data returned are:
According to him, he knows the page code.
It's not necessary to have a programming judgment.
Mb_detect_encoding judgment often misses, so added the Mb_check_encoding function
Data fragments
There's no reason for illegal characters.
CP936 is the international appellation of GBK
------to solve the idea----------------------
The first problem, not garbled, that is the picture, Curl crawl Baidu page, will deliberately convert some text into pictures, anti-crawling. You look at the page elements, you will find that those garbled is actually Baidu's image address.
The second problem, you set the timeout time to a larger point, just fine, it may be your network problem.