Curl collection garbled and not to collect PHP, how to deal with

Source: Internet
Author: User
Tags crc32 rfc unpack
Curl collection garbled and not to collect PHP
The PHP program is 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);//returns the text stream of the obtained output
$ret = curl_exec ($ch);
curl_setopt ($ch, curlopt_timeout, 1);
Curl_close ($ch);
Echo $ret;

?>

In the acquisition of sina.com.cn, is normal, but the acquisition of 163.com is empty, the acquisition of Sohu.com is lost code.
What's going on here? How to solve? What's the matter? Thank you first!!! Not a lot of points, sorry.

------Solution--------------------
Nothing else, I just came to get points. Landlord remember to give full points

PHP Code
$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 < | | 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 &! = $flags) {//Reserved bits is set--not allowed by RFC 1952 return null; }//Note: $mtime may negative (PHP integer limitations) $mtime =Unpack ("V", substr ($data, bis));     $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];    if ($len-$headerlen-2-$extralen < 8) {return false;      Invalid format} $extra = substr ($data, ten, $extralen);     $headerlen + = 2 + $extralen;     } $filenamelen = 0;     $filename = ""; if ($flags & 8) {//C-style string file NAME data in header if ($len-$headerlen-1 < 8) {Retu    RN false;      Invalid format} $filenamelen = Strpos (substr ($data, 8+ $extralen), Chr (0));    if ($filenamelen = = = False | | $len-$headerlen-$filenamelen-1 < 8) {return false; Invalid foRmat} $filename = substr ($data, $headerlen, $filenamelen);     $headerlen + = $filenamelen + 1;     } $commentlen = 0;     $comment = ""; if ($flags &) {//C-style string COMMENT data in header if ($len-$headerlen-1 < 8) {Retur    n false;      Invalid format} $commentlen = Strpos (substr ($data, 8+ $extralen + $filenamelen), Chr (0));    if ($commentlen = = = False | | $len-$headerlen-$commentlen-1 < 8) {return false;      Invalid header Format} $comment = substr ($data, $headerlen, $commentlen);     $headerlen + = $commentlen + 1;     } $HEADERCRC = "";        if ($flags & 1) {//2-bytes (lowest order) of CRC32 on header present if ($len-$headerlen-2 < 8) {    return false;      Invalid format} $CALCCRC = Crc32 (substr ($data, 0, $headerlen)) & 0xFFFF;      $HEADERCRC = Unpack ("V", substr ($data, $headerlen, 2));      $HEADERCRC = $HEADERCRC [1]; if ($HEADERCRC! = $cALCCRC) {return false;     Bad Header CRC} $headerlen + = 2;     }//GZIP Footer-these is negative due to PHP ' s limitations $DATACRC = Unpack ("V", substr ($data, -8,4));     $DATACRC = $DATACRC [1];     $isize = Unpack ("V", substr ($data,-4));       $isize = $isize [1];     Perform the decompression: $bodylen = $len-$headerlen-8;      if ($bodylen < 1) {//This should never happen-implementation bug!     return null;     } $body = substr ($data, $headerlen, $bodylen);     $data = "";           if ($bodylen > 0) {switch ($method) {case 8://Currently the only supported compression method:          $data = Gzinflate ($body);        Break      Default://Unknown compression method return false;      }} else {//I ' m not sure if zero-byte body content is allowed.  Allow it for now ...       Do nothing ...} verifiy decompressed size and CRC32://Note:this-fail with largE data sizes depending on how//PHP ' s integer limitations affect strlen () since $isize//could be Negati     ve for large sizes.  if ($isize! = strlen ($data) | | CRC32 ($data)! = $DATACRC) {//Bad format!      Length or CRC doesn ' t match!     return false;   } return $data; }
  • Related Article

    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.