The PHPPHP program uses gbk2312 encoding to collect garbled code from Curl and cannot collect the code: & lt ;? Php $ url & nbsp; www.sina.com.cn; gbk2312 encoding $ url & nbsp; www.163.com; gbk2312 encoding $ url & nbsp; www Curl collection garbled and cannot collect PHP
PHP programs are encoded with gbk2312:
$ Url = "http://www.sina.com.cn"; // gbk2312 encoding
// $ Url = "http://www.163.com"; // gbk2312 encoding
// $ Url = "http://www.sohu.com"; // gbk2312 encoding
$ Ch = curl_init ($ url );
Curl_setopt ($ ch, CURLOPT_RETURNTRANSFER, true); // return the obtained output text stream
$ Ret = curl_exec ($ ch );
Curl_setopt ($ ch, CURLOPT_TIMEOUT, 1 );
Curl_close ($ ch );
Echo $ ret;
?>
It is normal when sina.com.cn is collected, but it is null when 163.com is collected, and the code is lost when sohu.com is collected.
What's going on? How can this problem be solved? Why? Thank you !!! I'm sorry.
------ Solution --------------------
I just came to get the score.
$ Curl = curl_init ('http: // www.163.com ');
Curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, 1 );
Curl_setopt ($ curl, CURLOPT_USERAGENT, 'mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1;. net clr 1.1.4322 )');
$ Html = curl_exec ($ curl );
Var_dump ($ html );
$ Curl = curl_init ('http: // www.sohu.com ');
Curl_setopt ($ curl, CURLOPT_RETURNTRANSFER, 1 );
Curl_setopt ($ curl, CURLOPT_USERAGENT, 'mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.2; SV1;. net clr 1.1.4322 )');
$ Html = curl_exec ($ curl );
// $ Html = strstr ($ html, '<');
$ Html = gzdecode ($ html );
Var_dump ($ html );
Function gzdecode ($ data ){
$ Len = strlen ($ data );
If ($ len <18
------ Solution --------------------
Strcmp (substr ($ data, 0, 2), "\ x1f \ x8b ")){
Return null; // Not GZIP format (See RFC 1952)
}
$ Method = ord (substr ($ data, 2, 1); // Compression method
$ Flags = ord (substr ($ data, 3, 1); // Flags
If ($ flags & 31! = $ Flags ){
// Reserved bits are set -- not allowed by RFC 1952.
Return null;
}
// NOTE: $ mtime may be negative (PHP integer limitations)
$ Mtime = unpack ("V", substr ($ data, 4, 4 ));
$ Mtime = $ mtime [1];
$ Xfl = substr ($ data, 8, 1 );
$ OS = substr ($ data, 8, 1 );
$ Headerlen = 10;
$ Extralen = 0;
$ Extra = "";
If ($ flags & 4 ){
// 2-byte length prefixed EXTRA data in header
If ($ len-$ headerlen-2 <8 ){
Return false; // Invalid format
}
$ Extralen = unpack ("v", substr ($ data, 8, 2 ));
$ Extralen = $ extralen [1];