Curl-based garbage collection and failure to collect PHP code

Source: Internet
Author: User
Tags unpack
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];

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.